mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
fix(common): use locale NaN symbol in number formatting
Non-finite values all used NumberSymbol.Infinity, so formatNumber(NaN) rendered as infinity. Locale data already defines NumberSymbol.NaN.
This commit is contained in:
@@ -43,7 +43,11 @@ function formatNumberToLocaleString(
|
||||
let isZero = false;
|
||||
|
||||
if (!isFinite(value)) {
|
||||
formattedText = getLocaleNumberSymbol(locale, NumberSymbol.Infinity);
|
||||
// `Number.isNaN` (not `!isFinite`) so NaN uses the locale NaN symbol, not Infinity.
|
||||
formattedText = getLocaleNumberSymbol(
|
||||
locale,
|
||||
Number.isNaN(value) ? NumberSymbol.NaN : NumberSymbol.Infinity,
|
||||
);
|
||||
} else {
|
||||
let parsedNumber = parseNumber(value);
|
||||
|
||||
|
||||
@@ -38,6 +38,16 @@ describe('Format number', () => {
|
||||
expect(formatNumber(1e100, ɵDEFAULT_LOCALE_ID)).toEqual('1E+100');
|
||||
});
|
||||
|
||||
it('should format NaN and Infinity with locale symbols', () => {
|
||||
expect(formatNumber(NaN, ɵDEFAULT_LOCALE_ID)).toEqual('NaN');
|
||||
expect(formatNumber(Infinity, ɵDEFAULT_LOCALE_ID)).toEqual('∞');
|
||||
expect(formatNumber(-Infinity, ɵDEFAULT_LOCALE_ID)).toEqual('-∞');
|
||||
expect(formatPercent(NaN, ɵDEFAULT_LOCALE_ID)).toEqual('NaN%');
|
||||
expect(formatPercent(Infinity, ɵDEFAULT_LOCALE_ID)).toEqual('∞%');
|
||||
expect(formatCurrency(NaN, ɵDEFAULT_LOCALE_ID, '$')).toEqual('$NaN');
|
||||
expect(formatCurrency(Infinity, ɵDEFAULT_LOCALE_ID, '$')).toEqual('$∞');
|
||||
});
|
||||
|
||||
it('should throw if minFractionDigits is explicitly higher than maxFractionDigits', () => {
|
||||
expect(() => formatNumber(1.1, ɵDEFAULT_LOCALE_ID, '3.4-2')).toThrowError(
|
||||
/is higher than the maximum/,
|
||||
|
||||
Reference in New Issue
Block a user