Formatting numbers with thousand separators

264 Views Asked by At

Is there anything in the standard library or in Core that I can use to format integers with thousand separators?

2

There are 2 best solutions below

0
On BEST ANSWER

Unfortunately, nothing, expect that you can use the %a format specifier and provide your own pretty-printer.

0
On

You could use a %#d format to print an integer using underscores as separators (following OCaml lexical conventions):

# Printf.sprintf "=> %#d" 1000000;;
- : string = "=> 1_000_000"

And then replace underscores with commas:

# Printf.sprintf "=> %#d" 1000000 |> String.map (function '_' -> ',' | char -> char);;
- : string = "=> 1,000,000"