Prevent compact currency formatters from using trailing zeros for significant figures when currency has a minimum number of decimal places for non-compact representations.
PiperOrigin-RevId: 290354799
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e0658d4..e5f8823 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,8 @@
## 0.16.2
* Fix bug with dates in January being treated as ordinal. e.g. 2020-01-32 would
be accepted as valid and the day treated as day-of-year.
+ * Compact currency formats will avoid displaying unecessary trailing zeros
+ in compact formats for currencies which specify decimal places.
## 0.16.1
* Add an analysis_options.yaml and fix or suppress all the complaints from it.
diff --git a/lib/intl.dart b/lib/intl.dart
index 102e2dc..7116d5f 100644
--- a/lib/intl.dart
+++ b/lib/intl.dart
@@ -174,7 +174,7 @@
@pragma('dart2js:tryInline')
// We want dart2js to try to inline these messages, but not inline the
// internal messages, so it will eliminate the descriptions and other
- // information not needed at runtime.
+ // information not neeeded at runtime.
static String message(String messageText,
{String desc = '',
Map<String, Object> examples,
diff --git a/lib/src/intl/compact_number_format.dart b/lib/src/intl/compact_number_format.dart
index d696685..38b05f9 100644
--- a/lib/src/intl/compact_number_format.dart
+++ b/lib/src/intl/compact_number_format.dart
@@ -244,8 +244,8 @@
String format(number) {
_style = _styleFor(number);
- var divisor = _style.printsAsIs ? 1 : _style.divisor;
- var numberToFormat = _divide(number, divisor);
+ final divisor = _style.printsAsIs ? 1 : _style.divisor;
+ final numberToFormat = _divide(number, divisor);
var formatted = super.format(numberToFormat);
var prefix = _style.prefix;
var suffix = _style.suffix;
@@ -259,7 +259,7 @@
prefix = prefix.replaceFirst('\u00a4', currencySymbol);
suffix = suffix.replaceFirst('\u00a4', currencySymbol);
}
- var withExtras = '$prefix$formatted$suffix';
+ final withExtras = '$prefix$formatted$suffix';
_style = null;
return withExtras;
}
@@ -267,7 +267,7 @@
/// How many digits after the decimal place should we display, given that
/// there are [remainingSignificantDigits] left to show.
int _fractionDigitsAfter(int remainingSignificantDigits) {
- var newFractionDigits =
+ final newFractionDigits =
super._fractionDigitsAfter(remainingSignificantDigits);
// For non-currencies, or for currencies if the numbers are large enough to
// compact, always use the number of significant digits and ignore
@@ -284,6 +284,19 @@
}
}
+ /// Defines minimumFractionDigits based on current style being formatted.
+ @override
+ int get minimumFractionDigits {
+ if (!_isForCurrency ||
+ !significantDigitsInUse ||
+ _style == null ||
+ _style.isFallback) {
+ return super.minimumFractionDigits;
+ } else {
+ return 0;
+ }
+ }
+
/// Divide numbers that may not have a division operator (e.g. Int64).
///
/// Only used for powers of 10, so we require an integer denominator.
diff --git a/test/number_format_compact_test.dart b/test/number_format_compact_test.dart
index c89aa6a..91bf801 100644
--- a/test/number_format_compact_test.dart
+++ b/test/number_format_compact_test.dart
@@ -67,6 +67,7 @@
testCurrency('en_US', 12, r'$12.00', r'$10');
testCurrency('en_US', 12.3, r'$12.30', r'$10');
testCurrency('en_US', 123, r'$123', r'$100');
+ testCurrency('en_US', 1000, r'$1K', r'$1K');
testCurrency('en_US', 1234, r'$1.23K', r'$1K');
testCurrency('en_US', 12345, r'$12.3K', r'$10K');
testCurrency('en_US', 123456, r'$123K', r'$100K');