diff --git a/src/common/is-negative-zero.js b/src/common/is-negative-zero.js new file mode 100644 index 00000000..8741ad9d --- /dev/null +++ b/src/common/is-negative-zero.js @@ -0,0 +1,3 @@ +export default function isNegativeZero(value) { + return (1 / value === -Infinity); +} diff --git a/src/numbers/custom-number-format.js b/src/numbers/custom-number-format.js index e98f04b9..98bed114 100644 --- a/src/numbers/custom-number-format.js +++ b/src/numbers/custom-number-format.js @@ -1,4 +1,5 @@ import { CURRENCY, PERCENT, LIST_SEPARATOR, GROUP_SEPARATOR, CURRENCY_PLACEHOLDER, PERCENT_PLACEHOLDER, POINT, EMPTY } from '../common/constants'; +import isNegativeZero from '../common/is-negative-zero'; import formatCurrencySymbol from './format-currency-symbol'; import groupInteger from './group-integer'; import round from '../common/round'; @@ -273,7 +274,7 @@ export default function customNumberFormat(number, format, info) { const formatOptions = { negative: number < 0, number: Math.abs(number), - negativeZero: (1 / number === -Infinity), + negativeZero: isNegativeZero(number), format: format }; diff --git a/src/numbers/standard-number-format.js b/src/numbers/standard-number-format.js index 065ca057..50306484 100644 --- a/src/numbers/standard-number-format.js +++ b/src/numbers/standard-number-format.js @@ -1,4 +1,5 @@ import { PERCENT, SCIENTIFIC, NUMBER_PLACEHOLDER, CURRENCY_PLACEHOLDER, PERCENT_PLACEHOLDER, EMPTY, POINT } from '../common/constants'; +import isNegativeZero from '../common/is-negative-zero'; import formatCurrencySymbol from './format-currency-symbol'; import groupInteger from './group-integer'; import isCurrencyStyle from './is-currency-style'; @@ -95,7 +96,7 @@ export default function standardNumberFormat(number, options, info) { value = round(value, maximumFractionDigits); const negative = value < 0; - const negativeZero = (1 / number === -Infinity); + const negativeZero = isNegativeZero(number); const parts = value.split(POINT); diff --git a/test/numbers.js b/test/numbers.js index c30f003e..34914fb9 100644 --- a/test/numbers.js +++ b/test/numbers.js @@ -341,6 +341,10 @@ describe('standard accounting formatting', () => { delete cldr.custom; }); + it('should support minus zero', () => { + expect(formatNumber(-0, 'a2')).toEqual("($0.00)"); + }); + it('should apply format', () => { expect(formatNumber(10, 'a', 'custom')).toEqual("10.00$"); }); @@ -405,6 +409,10 @@ describe('custom formatting', () => { expect(formatNumber(10.9, '#')).toEqual("11"); }); + it('should support minus zero with custom format', () => { + expect(formatNumber(-0, '0.00;(0.00)')).toEqual("(0.00)"); + }); + it('replaces whole part of the number', () => { expect(formatNumber(-0, '#')).toEqual("-0"); });