Is there anything in the standard library or in Core that I can use to format integers with thousand separators?
Formatting numbers with thousand separators
272 Views Asked by Tobia At
2
There are 2 best solutions below
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"
Unfortunately, nothing, expect that you can use the
%aformat specifier and provide your own pretty-printer.