Skip to content

Commit 604bb08

Browse files
authored
fix: 0 decimals treated as zero when parsing with exceptZero (#8236)
* +X format fix in NumberParser * Fixed linting error * Formatting revert * Fixed testing error * Fixed linting errors (again)
1 parent 18912c1 commit 604bb08

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

packages/@internationalized/number/src/NumberParser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ class NumberParserImpl {
149149
// javascript is bad at dividing by 100 and maintaining the same significant figures, so perform it on the string before parsing
150150
let isNegative = fullySanitizedValue.indexOf('-');
151151
fullySanitizedValue = fullySanitizedValue.replace('-', '');
152+
fullySanitizedValue = fullySanitizedValue.replace('+', '');
152153
let index = fullySanitizedValue.indexOf('.');
153154
if (index === -1) {
154155
index = fullySanitizedValue.length;

packages/@internationalized/number/test/NumberParser.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,21 @@ describe('NumberParser', function () {
165165
});
166166
});
167167

168+
it('should parse a percent with signs', function () {
169+
expect(new NumberParser('en-GB', {style: 'percent', signDisplay: 'always'}).parse('+10%')).toBe(0.1);
170+
expect(new NumberParser('en-GB', {style: 'percent', signDisplay: 'always'}).parse('+0%')).toBe(0);
171+
expect(new NumberParser('en-GB', {style: 'percent', signDisplay: 'always'}).parse('-10%')).toBe(-0.1);
172+
expect(new NumberParser('en-GB', {style: 'percent', signDisplay: 'always'}).parse('-0%')).toBe(-0);
173+
expect(new NumberParser('en-US', {style: 'percent', signDisplay: 'exceptZero', minimumFractionDigits: 2}).parse('+0.50%')).toBe(0.005);
174+
});
175+
176+
it('should parse a percent with decimals and exceptZero', function () {
177+
expect(new NumberParser('en-GB', {style: 'percent', signDisplay: 'exceptZero'}).parse('+0.532%')).toBe(0.01);
178+
expect(new NumberParser('en-GB', {style: 'percent', signDisplay: 'exceptZero'}).parse('+0%')).toBe(0);
179+
expect(new NumberParser('en-GB', {style: 'percent', signDisplay: 'exceptZero'}).parse('0.532%')).toBe(0.01);
180+
expect(new NumberParser('en-GB', {style: 'percent', signDisplay: 'exceptZero'}).parse('-0.532%')).toBe(-0.01);
181+
});
182+
168183
describe('NumberFormat options', function () {
169184
it('supports roundingIncrement', function () {
170185
expect(new NumberParser('en-US', {roundingIncrement: 2}).parse('10')).toBe(10);

0 commit comments

Comments
 (0)