>base ( n radix -- str )
Factor documentation > Factor handbook > The language > Numbers > Converting between numbers and strings
Prev:>hex ( n -- str )
Next:string>number ( str -- n/f )


Vocabulary
math.parser

Inputs and outputs
na real
radixan integer between 2 and 16
stra string


Word description
Converts a real number into a string representation using the given radix. If the number is a float, the radix is ignored and the output is always in base 10.

See also
base>

Definition
IN: math.parser

GENERIC# >base 1 ( n radix -- str )


Methods
USING: combinators kernel math math.parser math.parser.private
;

M: float >base
{
{ [ over fp-nan? ] [ 2drop "0/0." ] }
{ [ over 1/0. = ] [ 2drop "1/0." ] }
{ [ over -1/0. = ] [ 2drop "-1/0." ] }
{ [ over 0.0 fp-bitwise= ] [ 2drop "0.0" ] }
{ [ over -0.0 fp-bitwise= ] [ 2drop "-0.0" ] }
[ float>base ]
} cond ;


USING: kernel math math.parser math.parser.private sequences ;

M: integer >base
over 0 =
[ 2drop "0" ] [
over 0 >
[ positive>base ]
[ [ neg ] dip positive>base 45 prefix ] if
] if ;


USING: kernel math math.parser math.parser.private sequences ;

M: ratio >base
[ [ 0 < ] [ abs 1 /mod ] bi ] [ [ positive>base ] curry ]
bi*
[ [ [ numerator ] [ denominator ] bi ] dip bi@ "/" glue ]
keep rot
[ drop ] [ swap call pick "-" "+" ? rot 3append ] if-zero
swap [ 45 prefix ] when ;