Fix bug with percents with no integer part
BUG=
R=tjblasi@google.com
Review URL: https://codereview.chromium.org//834313003
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 708eeb2..30acb85 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,11 @@
+## 0.12.0+1
+ * Fixes bug with printing a percent or permille format with no fraction
+ part and a number with no integer part. For example, print 0.12 with a
+ format pattern of "#%". The test for whether
+ there was a printable integer part tested the basic number, so it ignored the
+ integer digits. This was introduced in 0.11.2 when we stopped multiplying
+ the input number in the percent/permille case.
+
## 0.12.0
* Make withLocale and defaultLocale use a zone, so async operations
inside withLocale also get the correct locale. Bumping the version
diff --git a/lib/src/intl/number_format.dart b/lib/src/intl/number_format.dart
index 2e23d35..db4c7a4 100644
--- a/lib/src/intl/number_format.dart
+++ b/lib/src/intl/number_format.dart
@@ -310,7 +310,7 @@
var integerDigits = _integerDigits(integerPart, extraIntegerDigits);
var digitLength = integerDigits.length;
- if (_hasPrintableIntegerPart(integerPart)) {
+ if (_hasIntegerDigits(integerDigits)) {
_pad(minimumIntegerDigits - digitLength);
for (var i = 0; i < digitLength; i++) {
_addDigit(integerDigits.codeUnitAt(i));
@@ -386,11 +386,12 @@
/**
* Return true if we have a main integer part which is printable, either
- * because we have digits left of the decimal point, or because there are
- * a minimum number of printable digits greater than 1.
+ * because we have digits left of the decimal point (this may include digits
+ * which have been moved left because of percent or permille formatting),
+ * or because the minimum number of printable digits is greater than 1.
*/
- bool _hasPrintableIntegerPart(x) =>
- x > 0 || minimumIntegerDigits > 0;
+ bool _hasIntegerDigits(String digits) =>
+ digits.isNotEmpty || minimumIntegerDigits > 0;
/** A group of methods that provide support for writing digits and other
* required characters into [_buffer] easily.
diff --git a/pubspec.yaml b/pubspec.yaml
index a575d39..b1ad6c1 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
name: intl
-version: 0.12.0
+version: 0.12.0+1
author: Dart Team <misc@dartlang.org>
description: Contains code to deal with internationalized/localized messages, date and number formatting and parsing, bi-directional text, and other internationalization issues.
homepage: https://www.dartlang.org
@@ -27,5 +27,4 @@
- test/message_extraction/message_extraction_no_deferred_test.dart
- test/message_extraction/message_extraction_test.dart
- test/message_extraction/really_fail_extraction_test.dart
- - test/message_extraction/really_fail_extraction_test.dart
- test/intl_message_basic_example_test.dart # invalid import under pub's package-layout
diff --git a/test/number_format_test.dart b/test/number_format_test.dart
index 1ff898b..b6bc33a 100644
--- a/test/number_format_test.dart
+++ b/test/number_format_test.dart
@@ -143,6 +143,14 @@
}
});
+ test('Percent with no decimals and no integer part', () {
+ var number = new NumberFormat("#%");
+ var formatted = number.format(0.12);
+ expect(formatted, "12%");
+ var readBack = number.parse(formatted);
+ expect(0.12, readBack);
+ });
+
// We can't do these in the normal tests because those also format the
// numbers and we're reading them in a format where they won't print
// back the same way.