diff --git a/rslib/i18n/src/lib.rs b/rslib/i18n/src/lib.rs index f8e483782..7c78cf71c 100644 --- a/rslib/i18n/src/lib.rs +++ b/rslib/i18n/src/lib.rs @@ -22,13 +22,46 @@ type FluentBundle = FluentBundleOrig {} -impl Number for i32 {} -impl Number for i64 {} -impl Number for u32 {} -impl Number for f32 {} -impl Number for u64 {} -impl Number for usize {} +pub trait Number: Into { + fn round(self) -> Self; +} +impl Number for i32 { + #[inline] + fn round(self) -> Self { + self + } +} +impl Number for i64 { + #[inline] + fn round(self) -> Self { + self + } +} +impl Number for u32 { + #[inline] + fn round(self) -> Self { + self + } +} +impl Number for f32 { + // round to 2 decimal places + #[inline] + fn round(self) -> Self { + (self * 100.0).round() / 100.0 + } +} +impl Number for u64 { + #[inline] + fn round(self) -> Self { + self + } +} +impl Number for usize { + #[inline] + fn round(self) -> Self { + self + } +} fn remapped_lang_name(lang: &LanguageIdentifier) -> &str { let region = lang.region.as_ref().map(|v| v.as_str()); @@ -446,6 +479,14 @@ mod test { assert!(want_comma_as_decimal_separator(&[langid!("pl-PL")])); } + #[test] + fn decimal_rounding() { + let tr = I18n::new(&["en"]); + + assert_eq!(tr.browsing_cards_deleted(1.001), "1 card deleted."); + assert_eq!(tr.browsing_cards_deleted(1.01), "1.01 cards deleted."); + } + #[test] fn i18n() { // English template diff --git a/rslib/i18n/write_strings.rs b/rslib/i18n/write_strings.rs index 009d519e5..f9df5716f 100644 --- a/rslib/i18n/write_strings.rs +++ b/rslib/i18n/write_strings.rs @@ -97,7 +97,7 @@ fn build_vars(translation: &Translation) -> String { let rust_name = v.name.to_snake_case(); let trailer = match v.kind { VariableKind::Any => "", - VariableKind::Int | VariableKind::Float => ".into()", + VariableKind::Int | VariableKind::Float => ".round().into()", VariableKind::String => ".into()", }; writeln!(