Skip to content
Browse files

add Int.base(), run test.

Cribbed from rakudo's ng branch.
  • Loading branch information...
1 parent 04caf92 commit 8fa1154e1c5f3f7416f80268a9651904702bb216 @coke coke committed Jan 5, 2012
Showing with 15 additions and 0 deletions.
  1. +14 −0 lib/CORE.setting
  2. +1 −0 t/spectest.data
View
14 lib/CORE.setting
@@ -651,6 +651,20 @@ my class Int does Integral {
method Int() { self }
method perl() { defined(self) ?? ~self !! self.typename }
method FatRat() { FatRat.new(self, 1) }
+ method base(Cool $base) {
+ my $intBase = $base;
+ die("base must be between 2 and 36, got $base")
+ unless 2 <= $intBase <= 36;
+ my @conversion = 0..9, 'A' .. 'Z';
+ my @res;
+ my $n = self.abs;
+ repeat {
+ push @res, @conversion[$n % $intBase];
+ $n div= $intBase;
+ } while $n > 0;
+ push @res, '-' if self < 0;
+ join '', @res.reverse;
+ }
}
my class Rat does Real {
method new($n,$d) { $n / $d }
View
1 t/spectest.data
@@ -408,6 +408,7 @@ S32-list/reverse.t
S32-list/roll.t
S32-list/sort.t
S32-num/abs.t
+S32-num/base.t
S32-num/complex.t
S32-num/cool-num.t
S32-num/exp.t

0 comments on commit 8fa1154

Please sign in to comment.
Something went wrong with that request. Please try again.