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. Commented Nov 3, 2021 at 9:42
  • @RaphaëlCauderlier I don't, someone else asked me about it, which piqued my interest. Commented Nov 3, 2021 at 9:53

1 Answer 1

0

Unfortunately, there is no such facility in CameLIGO.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.