Ocaml - How Do I Convert Int to String?

How do I convert an int to a string? Example: 1 to "1".

0

2 Answers

Use the function string_of_int (see the documentation for Pervasives, the module containing the functions that are automatically made available in the top level namespace to al OCaml programs).

0

Another solution is to use the module Printf, which allows you to choose the format of printing:

Printf.sprintf "%d" 42

gives you "42".

But you might prefer using an octal, hexadecimal, binary, etc representation. For instance,

Printf.sprintf "%x" 42

gives you "2a" which is the hexadecimal representation of 42.

Printf.sprintf "0x%x" 42

would give you "0x2a".

See the Printf documentation for more details.

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.

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest