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

Commit a13c01a

Browse files
pmeskersIgorMinar
authored andcommitted
fix(numberFilter): always convert scientific notation to decimal
Previously, the number filter would format small and large numbers as scientific notation. It now uses toFixed() to ensure that all requested digits are shown.
1 parent 454bcfa commit a13c01a

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/ng/filter/filters.js

+5
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ function formatNumber(number, pattern, groupSep, decimalSep, fractionSize) {
171171
}
172172

173173
if (fractionSize && fractionSize !== "0") formatedText += decimalSep + fraction.substr(0, fractionSize);
174+
} else {
175+
176+
if (fractionSize > 0 && number > -1 && number < 1) {
177+
formatedText = number.toFixed(fractionSize);
178+
}
174179
}
175180

176181
parts.push(isNegative ? pattern.negPre : pattern.posPre);

test/ng/filter/filtersSpec.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,24 @@ describe('filters', function() {
145145
expect(number(1234.567, 2)).toEqual("1,234.57");
146146
});
147147

148-
it('should filter exponential numbers', function() {
149-
expect(number(1e50, 0)).toEqual('1e+50');
150-
expect(number(-2e50, 2)).toEqual('-2e+50');
148+
it('should filter exponentially large numbers', function() {
149+
expect(number(1e50)).toEqual('1e+50');
150+
expect(number(-2e100)).toEqual('-2e+100');
151+
});
152+
153+
it('should ignore fraction sizes for large numbers', function() {
154+
expect(number(1e50, 2)).toEqual('1e+50');
155+
expect(number(-2e100, 5)).toEqual('-2e+100');
156+
});
157+
158+
it('should filter exponentially small numbers', function() {
159+
expect(number(1e-50, 0)).toEqual('0');
160+
expect(number(1e-6, 6)).toEqual('0.000001');
161+
expect(number(1e-7, 6)).toEqual('0.000000');
162+
163+
expect(number(-1e-50, 0)).toEqual('-0');
164+
expect(number(-1e-6, 6)).toEqual('-0.000001');
165+
expect(number(-1e-7, 6)).toEqual('-0.000000');
151166
});
152167
});
153168

0 commit comments

Comments
 (0)