pub fn format_number(
value: f64,
locale: &str,
decimals: Option<usize>,
) -> StringExpand description
Format a number with the decimal point and thousands separator of
a locale. Currency symbols live in format_currency.
Only the base language counts, so "en-US" acts as "en". An
unknown locale falls back to "en" and logs a warning.
decimals of None keeps the input’s own precision. Some(n)
pads or cuts the fraction to exactly n digits.
Separators by language:
- en, ja, zh, ko, th:
.decimal,,groups - de, es, it, nl, pt, pl, sv, nb, nn, da, fi, el, tr:
,decimal,.groups - fr, ru, cs, sk, bg, uk, hu:
,decimal, space groups
As a Tera filter it takes the same locale and decimals
arguments, and passes non-numeric input through unchanged:
{{ 1234567.89 | format_number(locale="de") }} {# → 1.234.567,89 #}
{{ 1234.5 | format_number(locale="en", decimals=2) }} {# → 1,234.50 #}use rustango::humanize::format_number;
assert_eq!(format_number(1234567.89, "en", None), "1,234,567.89");
assert_eq!(format_number(1234567.89, "de", None), "1.234.567,89");
assert_eq!(format_number(1234567.89, "fr", None), "1 234 567,89");
assert_eq!(format_number(1234.5, "en", Some(2)), "1,234.50");
assert_eq!(format_number(0.0, "en", Some(0)), "0");
assert_eq!(format_number(-1234.5, "en", None), "-1,234.5");