Thousands separator

is there a macro to format numeric output with commas like

2000 into 2,000

2 Likes

You can use an underscore for this.

Formatting with commas is actually a bit more complex than it seems because different countries write numbers differently (e.g. an American might write 20 million as 20,000,000.0 while a German might write 20.000.000,0 and an Indian might write 2,00,00,000). That means naively adding some comma formatter to std::fmt (e.g. println!("{:,}", 2000)) would produce the "wrong" output for a decent chunk of the world.

Managing this correctly requires you to be aware of the current locale and you often do that by calling OS APIs. This presents an issue for the standard library because now all your formatting infrastructure is dependent on the OS, but Rust is a systems language and our formatting infrastructure is in core (the OS-agnostic part of the standard library) so we don't really want it to have any dependencies on OS functionality. I think it was decided very early on that localisation is out of scope for the standard library.

The best solution is to find a crate that either does provide locale-aware formatting (I don't know of any off the top of my head), or alternately something like thousands or num-format which will generate the strings you are looking for.

See also:

16 Likes

Even worse, house rules might say, even for, say, US locale, that four digits doesn't get a comma (eg 10,000 but only 1000), that negative money amounts should be -$123, and all sorts of other nightmares.

And let's not get anywhere near the rest of locales...

4 Likes

not sure if this is the right place but i hope the next iteration of Rust will include thousands separator with some switch to indicate locale (US, UK, Indian, etc.,) by default.

it's a very common requirement that it's a waste of time to search the Internet for a way to do it in Rust.

I know this is not what you meant but it prompted me to search for a crate called "underscore".

Which lead me to this entry on crates.io https://crates.io/crates/underscore.

There is no documentation there as the crate has no README.

Following the documentation link on that crates.io page leads to some advertising for casinos and other junk.

Is there any way to get such spam entries deleted from crates.io?

2 Likes

Typically such suggestions are met with the response that such functionality should be provided by a crate (or crates) rather than being baked into the language or its standard library. I tend to agree with that.

Rust is a language for creating such functionality not providing it ready made. Once someone has created a locale formatting crate as you require it would be available to all anyway so no need to have it in the language out of the box.

Question is, is there such a crate?

1 Like

Doing internationalization and localization correctly is hard.

I'm very glad that Rust's standard library doesn't provide some half-baked partial support, and instead encourages the use of something provided by experts -- like the official unicode GitHub - unicode-org/icu4x: Solving i18n for client-side and resource-constrained environments. project that's even written in Rust.

Specifically, for things like decimal separators it looks like you want https://icu4x.unicode.org/doc/icu_decimal/index.html#examples.

8 Likes

I reported the crate on zulip.

1 Like