Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix(formatNumber): allow negative fraction size #13913

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions src/ng/filter/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,18 +221,37 @@ function roundNumber(parsedNumber, fractionSize, minFrac, maxFrac) {
var digit = digits[roundAt];

if (roundAt > 0) {
digits.splice(roundAt);
// Drop fractional digits beyond `roundAt`
digits.splice(Math.max(parsedNumber.i, roundAt));

// Set non-fractional digits beyond `roundAt` to 0
for (var j = roundAt; j < digits.length; j++) {
digits[j] = 0;
}
} else {
// We rounded to zero so reset the parsedNumber
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inserting the following line here, solves problem (B) described in https://github.com/angular/angular.js/pull/13913/files#r51409840:

fractionLen = Math.max(0, fractionLen);

fractionLen = Math.max(0, fractionLen);
parsedNumber.i = 1;
digits.length = roundAt = fractionSize + 1;
for (var i=0; i < roundAt; i++) digits[i] = 0;
digits.length = Math.max(1, roundAt = fractionSize + 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICT, fractionSize + 1 can never be > 1, because:

fractionSize + 1 > 1 <=>
fractionSize > 0     <=>
fractionSize + parsedNumber.i > 0 (because `parsedNumber.i > 0`) <=>
roundedAt > 0 (on line 220) <=>
We wouldn't be on this branch of the if-else block

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm...it seems that parsedNumber.i can be < 1, so ignore the above :)

digits[0] = 0;
for (var i = 1; i < roundAt; i++) digits[i] = 0;
}

if (digit >= 5) digits[roundAt - 1]++;
if (digit >= 5) {
if (roundAt - 1 < 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inserting the following lines here, solves problem (A) described in https://github.com/angular/angular.js/pull/13913/files#r51409840:

for (var k = 0; k > roundAt; k--) {
  digits.unshift(0);
  parsedNumber.i++;
}

for (var k = 0; k > roundAt; k--) {
digits.unshift(0);
parsedNumber.i++;
}
digits.unshift(1);
parsedNumber.i++;
} else {
digits[roundAt - 1]++;
}
}

// Pad out with zeros to get the required fraction length
for (; fractionLen < fractionSize; fractionLen++) digits.push(0);
for (; fractionLen < Math.max(0, fractionSize); fractionLen++) digits.push(0);


// Do any carrying, e.g. a digit was rounded up to 10
Expand Down
20 changes: 20 additions & 0 deletions test/ng/filter/filtersSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ describe('filters', function() {
expect(num).toBe('123.100');
});

it('should work with negative fractionSize', function() {
expect(formatNumber(49, pattern, ',', '.', -2)).toBe('0');
expect(formatNumber(50, pattern, ',', '.', -2)).toBe('100');
expect(formatNumber(51, pattern, ',', '.', -2)).toBe('100');
expect(formatNumber(1234, pattern, ',', '.', -1)).toBe('1,230');
expect(formatNumber(1234.567, pattern, ',', '.', -1)).toBe('1,230');
expect(formatNumber(1235, pattern, ',', '.', -1)).toBe('1,240');
expect(formatNumber(1235, pattern, ',', '.', -2)).toBe('1,200');
expect(formatNumber(1235, pattern, ',', '.', -3)).toBe('1,000');
expect(formatNumber(1235, pattern, ',', '.', -4)).toBe('0');
expect(formatNumber(1250, pattern, ',', '.', -2)).toBe('1,300');
expect(formatNumber(1000, pattern, ',', '.', -3)).toBe('1,000');
expect(formatNumber(1000, pattern, ',', '.', -4)).toBe('0');
expect(formatNumber(1000, pattern, ',', '.', -5)).toBe('0');
expect(formatNumber(1, pattern, ',', '.', -1)).toBe('0');
expect(formatNumber(1, pattern, ',', '.', -2)).toBe('0');
expect(formatNumber(9, pattern, ',', '.', -1)).toBe('10');
expect(formatNumber(501, pattern, ',', '.', -3)).toBe('1,000');
});

it('should format numbers that round to zero as nonnegative', function() {
expect(formatNumber(-0.01, pattern, ',', '.', 1)).toBe('0.0');
expect(formatNumber(-1e-10, pattern, ',', '.', 1)).toBe('0.0');
Expand Down