In role Real§
See primary documentation in context for method base
method base(Real:D: Int:D $base where 2..36, $digits? --> Str:D)
Converts the number to a string, using $base
as base. For $base
larger than ten, capital Latin letters are used.
255.base(16); # 'FF'
The optional $digits
argument asks for that many digits of fraction (which may not be negative). If omitted, a reasonable default is chosen based on type. For Int this default is 0. For Num
, the default is 8. For Rational
, the number of places is scaled to the size of the denominator, with a minimum of 6.
A special value of Whatever
(*
) can be given as $digits
, which functions the same as when $digits
is not specified for all Real
types except the Rational
s. For Rational
s, the Whatever
indicates that you wish all of the possible digits of the fractional part, but use caution: since there's no detection of repeating fractional parts (the algorithm will eventually stop after generating 2**63 digits).
The final digit produced is always rounded.
say pi.base(10, 3); # OUTPUT: «3.142» say (1/128).base(10, *); # OUTPUT: «0.0078125» say (1/100).base(10, *); # OUTPUT: «0.01» say (1/3) .base(10, *); # WRONG: endlessly repeating fractional part
For reverse operation, see parse-base