allow specifying the number of decimal digits (#484)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8a00c8d..e1571b3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,8 @@
* Fix negative number formatting / parsing in `NumberFormat.compact()`.
* Add optional parameter to `NumberFormat.compact()` to explicitly add sign
even for positive values.
+ * Add `decimalPatternDigits` to NumberFormat which supports specifying the
+ number of decimal digits in a decimal pattern.
* Update to cldr 40.
* Migrate to `package:lints/recommended.yaml`.
* Remove some instances of dynamic types from the API.
diff --git a/lib/src/intl/number_format.dart b/lib/src/intl/number_format.dart
index f45d4f9..e76dd64 100644
--- a/lib/src/intl/number_format.dart
+++ b/lib/src/intl/number_format.dart
@@ -210,6 +210,12 @@
factory NumberFormat.decimalPattern([String? locale]) =>
NumberFormat._forPattern(locale, (x) => x.DECIMAL_PATTERN);
+ /// Create a number format that prints as DECIMAL_PATTERN.
+ factory NumberFormat.decimalPatternDigits(
+ {String? locale, int? decimalDigits}) =>
+ NumberFormat._forPattern(locale, (x) => x.DECIMAL_PATTERN,
+ decimalDigits: decimalDigits);
+
/// Create a number format that prints as PERCENT_PATTERN.
factory NumberFormat.percentPattern([String? locale]) =>
NumberFormat._forPattern(locale, (x) => x.PERCENT_PATTERN);
diff --git a/test/number_format_test_core.dart b/test/number_format_test_core.dart
index 9284929..9ceefff 100644
--- a/test/number_format_test_core.dart
+++ b/test/number_format_test_core.dart
@@ -448,21 +448,35 @@
expect(() => format.parse('-∞+1'), throwsFormatException);
});
- var digitsCheck = {
- 0: '@4',
- 1: '@4.3',
- 2: '@4.32',
- 3: '@4.322',
- 4: '@4.3220',
- };
+ test('Decimal digits for decimal pattern', () {
+ const number = 4.3219876;
+ void expectDigits(String locale, List<String> expectations) {
+ for (var index = 0; index < expectations.length; index++) {
+ var format = NumberFormat.decimalPatternDigits(
+ locale: locale, decimalDigits: index);
+ expect(format.format(number), expectations[index]);
+ }
+ }
- test('Decimal digits', () {
+ expectDigits('en_US', ['4', '4.3', '4.32', '4.322', '4.3220']);
+ expectDigits('de_DE', ['4', '4,3', '4,32', '4,322', '4,3220']);
+ });
+
+ test('Decimal digits for currency', () {
+ const digitsCheck = [
+ '@4',
+ '@4.3',
+ '@4.32',
+ '@4.322',
+ '@4.3220',
+ ];
+
var amount = 4.3219876;
- for (var digits in digitsCheck.keys) {
- var f = NumberFormat.currency(
- locale: 'en_US', symbol: '@', decimalDigits: digits);
- var formatted = f.format(amount);
- expect(formatted, digitsCheck[digits]);
+ for (var index = 0; index < digitsCheck.length; index++) {
+ var format = NumberFormat.currency(
+ locale: 'en_US', symbol: '@', decimalDigits: index);
+ var formatted = format.format(amount);
+ expect(formatted, digitsCheck[index]);
}
var defaultFormat = NumberFormat.currency(locale: 'en_US', symbol: '@');
var formatted = defaultFormat.format(amount);