Tobia
Tobia

Reputation: 18811

Formatting numbers with thousand separators

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

Upvotes: 3

Views: 282

Answers (2)

Vladimir Keleshev
Vladimir Keleshev

Reputation: 14295

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"

Upvotes: 3

ivg
ivg

Reputation: 35280

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

Upvotes: 2

Related Questions