Is f64::to_string localized?

If I do the following...

let text = 10.5f64.to_string();
println!("{}", text);

...will I always see 10.5 or can I see 10,5 depending on the language the OS is using?

It always prints 10.5 and does not depend on the locale.

4 Likes

On this subject: looking into this question, I was surprised how hard it was to get an up-or-down answer from the standard library documentation. Even a detailed page like the module-level documentation for std::fmt doesn't appear to mention the existence of locale-specific conventions and clarify that Rust doesn't vary behavior by locale, when this is a natural question coming from other languages.

2 Likes

The concept of a "locale" doesn't even exist in Rust. That's an operating system concept, whereas rust is the sort of language that can be used to write operating systems.

At present, even the standard library of Rust (where abstractions over the operating system go) does not expose an interface for working with locales.

3 Likes

For number formatting I found this num-format crate that looks like it should do what you're looking for. Searching crates.io came up with a few crates, and here is a search link for "locale" on libs.rs that lists some more crates.

1 Like

Thanks everyone. It's as I had thought but I needed to make sure.

1 Like

To put my first comment another way: I'm surprised the module documentation doesn't bring up the subject and say what you said directly, when many people like OP will wonder about this.

2 Likes

I opened an issue about this.

4 Likes

It always prints 10.5 and does not depend on the locale.

Thank you very much for this sanity. For a lot of things that need to be deterministic everywhere, it's my experience that it is frustrating that C's string functions are locale sensitive by default.
Using native locales for user interaction is good, don't get me wrong, but that should be a choice.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.