HiveBrain v1.2.0
Get Started
← Back to all entries
patternrustModerate

Is it possible to print a number formatted with thousand separator in Rust?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
possiblewithprintthousandnumberrustformattedseparator

Problem

For instance

println!("{}", 10_000_000);


results in

10000000


whereas I'd like to format it to look something like

10,000,000


I went through the fmt module documentation, but there's nothing to cover this particular situation. I thought something like this would work

println!("{:,i}", 10_000_000);


but it throws an error

invalid format string: expected }, found ,

Solution

The num_format crate will solve this issue for you. Add your locale and it will do the magic.

Context

Stack Overflow Q#26998485, score: 29

Revisions (0)

No revisions yet.