0

Is there a more sensible way to convert an int to a string in Cameligo?

Alternatively, is there a way to concatenate an int or nat with a string?

type lookup = (int, string) map
let lookup: lookup = Map.literal[(0, "0"); (1, "1"); (2, "2"); (3, "3"); (4, "4"); (5, "5"); (6, "6"); (7, "7"); (8, "8"); (9, "9")]

let digitToStr (key: int): string =
  match Map.find_opt key lookup with
    Some str -> str
  | None -> (failwith "Not found": string)

let rec intToStr ((x, str): int * string) : string =
    if (x < 10) then 
      digitToStr(x) ^ str 
    else
      intToStr((
        x / 10, 
        digitToStr(int(x mod 10)) ^ str
      ))

let main (x: int) : string =
    intToStr((x, ""))
2
  • Why do you want a string? It is usually simpler to keep your onchain data structured and convert to string offchain. Nov 3, 2021 at 9:42
  • @RaphaëlCauderlier I don't, someone else asked me about it, which piqued my interest. Nov 3, 2021 at 9:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.