| use std::sync::OnceLock; | |
| use icu_decimal::DecimalFormatter; | |
| use icu_decimal::input::Decimal; | |
| use icu_decimal::options::DecimalFormatterOptions; | |
| use icu_locale_core::Locale; | |
| fn make_local_formatter() -> Option<DecimalFormatter> { | |
| let loc: Locale = sys_locale::get_locale()?.parse().ok()?; | |
| DecimalFormatter::try_new(loc.into(), DecimalFormatterOptions::default()).ok() | |
| } | |
| fn make_en_us_formatter() -> DecimalFormatter { | |
| let loc: Locale = "en-US".parse().expect("en-US wasn't a valid locale"); | |
| DecimalFormatter::try_new(loc.into(), DecimalFormatterOptions::default()) | |
| .expect("en-US wasn't a valid locale") | |
| } | |
| fn formatter() -> &'static DecimalFormatter { | |
| static FORMATTER: OnceLock<DecimalFormatter> = OnceLock::new(); | |
| FORMATTER.get_or_init(|| make_local_formatter().unwrap_or_else(make_en_us_formatter)) | |
| } | |
| /// Format an i64 with locale-aware digit separators (e.g. "12345" -> "12,345" | |
| /// for en-US). | |
| pub fn format_with_separators(n: i64) -> String { | |
| formatter().format(&Decimal::from(n)).to_string() | |
| } | |