From 602f7e7b6c5bebd500a944b9d9d423fccac06bfd Mon Sep 17 00:00:00 2001 From: appflowy Date: Mon, 26 Sep 2022 17:33:31 +0800 Subject: [PATCH] fix: format number in percent format --- .../field/type_option/number_format_bloc.dart | 7 +++++-- .../field/type_options/number_type_option/format.rs | 11 ++++++++++- .../type_options/number_type_option/number_tests.rs | 5 +++++ .../number_type_option/number_type_option.rs | 2 +- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/frontend/app_flowy/lib/plugins/grid/application/field/type_option/number_format_bloc.dart b/frontend/app_flowy/lib/plugins/grid/application/field/type_option/number_format_bloc.dart index 7086f3385a..870eb2cdef 100644 --- a/frontend/app_flowy/lib/plugins/grid/application/field/type_option/number_format_bloc.dart +++ b/frontend/app_flowy/lib/plugins/grid/application/field/type_option/number_format_bloc.dart @@ -11,7 +11,10 @@ class NumberFormatBloc extends Bloc { event.map(setFilter: (_SetFilter value) { final List formats = List.from(NumberFormat.values); if (value.filter.isNotEmpty) { - formats.retainWhere((element) => element.title().toLowerCase().contains(value.filter.toLowerCase())); + formats.retainWhere((element) => element + .title() + .toLowerCase() + .contains(value.filter.toLowerCase())); } emit(state.copyWith(formats: formats, filter: value.filter)); }); @@ -91,7 +94,7 @@ extension NumberFormatExtension on NumberFormat { case NumberFormat.Percent: return "Percent"; case NumberFormat.PhilippinePeso: - return "Percent"; + return "PhilippinePeso"; case NumberFormat.Pound: return "Pound"; case NumberFormat.Rand: diff --git a/frontend/rust-lib/flowy-grid/src/services/field/type_options/number_type_option/format.rs b/frontend/rust-lib/flowy-grid/src/services/field/type_options/number_type_option/format.rs index 00de383096..0a8029e869 100644 --- a/frontend/rust-lib/flowy-grid/src/services/field/type_options/number_type_option/format.rs +++ b/frontend/rust-lib/flowy-grid/src/services/field/type_options/number_type_option/format.rs @@ -70,6 +70,15 @@ define_currency_set!( symbol: "RUB", symbol_first: false, }, + PERCENT : { + code: "", + exponent: 2, + locale: EnIn, + minor_units: 1, + name: "percent", + symbol: "%", + symbol_first: false, + }, USD : { code: "USD", exponent: 2, @@ -435,7 +444,7 @@ impl NumberFormat { NumberFormat::Leu => number_currency::RON, NumberFormat::ArgentinePeso => number_currency::ARS, NumberFormat::UruguayanPeso => number_currency::UYU, - NumberFormat::Percent => number_currency::USD, + NumberFormat::Percent => number_currency::PERCENT, } } diff --git a/frontend/rust-lib/flowy-grid/src/services/field/type_options/number_type_option/number_tests.rs b/frontend/rust-lib/flowy-grid/src/services/field/type_options/number_type_option/number_tests.rs index 41d4cb212c..f92e8c6d90 100644 --- a/frontend/rust-lib/flowy-grid/src/services/field/type_options/number_type_option/number_tests.rs +++ b/frontend/rust-lib/flowy-grid/src/services/field/type_options/number_type_option/number_tests.rs @@ -93,6 +93,11 @@ mod tests { assert_number(&type_option, "€0.5", "€0,5", &field_type, &field_rev); assert_number(&type_option, "€1844", "€1.844", &field_type, &field_rev); } + NumberFormat::Percent => { + assert_number(&type_option, "1", "1%", &field_type, &field_rev); + assert_number(&type_option, "10.1", "10.1%", &field_type, &field_rev); + assert_number(&type_option, "100", "100%", &field_type, &field_rev); + } _ => {} } } diff --git a/frontend/rust-lib/flowy-grid/src/services/field/type_options/number_type_option/number_type_option.rs b/frontend/rust-lib/flowy-grid/src/services/field/type_options/number_type_option/number_type_option.rs index fe4ddcf5fa..984e7561bc 100644 --- a/frontend/rust-lib/flowy-grid/src/services/field/type_options/number_type_option/number_type_option.rs +++ b/frontend/rust-lib/flowy-grid/src/services/field/type_options/number_type_option/number_type_option.rs @@ -77,7 +77,7 @@ impl NumberTypeOptionPB { pub(crate) fn format_cell_data(&self, s: &str) -> FlowyResult { match self.format { - NumberFormat::Num | NumberFormat::Percent => match Decimal::from_str(s) { + NumberFormat::Num => match Decimal::from_str(s) { Ok(value, ..) => Ok(NumberCellData::from_decimal(value)), Err(_) => Ok(NumberCellData::new()), },