How to change format decimal separator in Rust?

The function below leads to "10.000". Where I live, it means ten thousand.

format!("{:.3}", 10.0);

I would like the result to be "10,000."

+4
source share
3 answers

Since the standard library does not have this function (localization of the number format), you can simply replace the dot with a semicolon:

fn main() {
    println!("{}", format!("{:.3}", 10.0).replacen(".", ",", 1));
}

There are other ways to do this, but this is probably the easiest solution.

+4
source

There is no support for internationalization (i18n) or localization (l10n), baked in the Rust standard library.


In one particular order, there are several reasons:

  • the locale-dependent output should be a conscious choice, not a standard one,
  • i18n l10n , ,
  • Rust std .

format! JSON XML. , , . .

. ( ), , .

. , , , . , , , ... , (, , / ). , .. , , ( gettext C, ).

, , -. , Facebook , .

, //... . ICU ( ?) . std ; , , .

, , ... .

+4

This is not a macro role format!. This parameter must be handled by Rust. Unfortunately, my search led me to conclude that Rust does not process the locale (yet?).

There is a rust-locale library , but they are still in alpha.

+1
source

Source: https://habr.com/ru/post/1679382/


All Articles