Reputation: 265
I am in need of a function, say, int2string, that can convert a given integer into its string representation. If possible, int2string may take a second argument referring to the base of the representation. By default, this second argument is 10.
Upvotes: 2
Views: 231
Reputation:
To tostring_int
. It's available in prelude/SATS/tostring.sats
and you can overload tostring
or something if you'd like :)
Upvotes: 1
Reputation: 559
This works - although it may not be the best style. Please feel free to refine this answer.
extern
fun
int2string(i: int, b: int): string
(* ****** ****** *)
implement
int2string(i, b) = let
val () = assertloc(i >= 0)
fun dig2str(i:int): string =
if i = 0 then "0"
else if i = 1 then "1"
else if i = 2 then "2"
else if i = 3 then "3"
else if i = 4 then "4"
else if i = 5 then "5"
else if i = 6 then "6"
else if i = 7 then "7"
else if i = 8 then "8"
else if i = 9 then "9"
else "" // can add A, B, C,...
fun helper(i: int, res: string): string =
if i > 0 then helper(i / b, dig2str(i % b) + res)
else res
in
helper(i, "")
end
Upvotes: 0