Skip to content

Commit 7da0ea4

Browse files
chore: extract isnegativezero
1 parent bf7a72b commit 7da0ea4

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

src/common/is-negative-zero.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function isNegativeZero(value) {
2+
return (1 / value === -Infinity);
3+
}

src/numbers/custom-number-format.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { CURRENCY, PERCENT, LIST_SEPARATOR, GROUP_SEPARATOR, CURRENCY_PLACEHOLDER, PERCENT_PLACEHOLDER, POINT, EMPTY } from '../common/constants';
2+
import isNegativeZero from '../common/is-negative-zero';
23
import formatCurrencySymbol from './format-currency-symbol';
34
import groupInteger from './group-integer';
45
import round from '../common/round';
@@ -273,7 +274,7 @@ export default function customNumberFormat(number, format, info) {
273274
const formatOptions = {
274275
negative: number < 0,
275276
number: Math.abs(number),
276-
negativeZero: (1 / number === -Infinity),
277+
negativeZero: isNegativeZero(number),
277278
format: format
278279
};
279280

src/numbers/standard-number-format.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { PERCENT, SCIENTIFIC, NUMBER_PLACEHOLDER, CURRENCY_PLACEHOLDER, PERCENT_PLACEHOLDER, EMPTY, POINT } from '../common/constants';
2+
import isNegativeZero from '../common/is-negative-zero';
23
import formatCurrencySymbol from './format-currency-symbol';
34
import groupInteger from './group-integer';
45
import isCurrencyStyle from './is-currency-style';
@@ -95,7 +96,7 @@ export default function standardNumberFormat(number, options, info) {
9596
value = round(value, maximumFractionDigits);
9697

9798
const negative = value < 0;
98-
const negativeZero = (1 / number === -Infinity);
99+
const negativeZero = isNegativeZero(number);
99100

100101
const parts = value.split(POINT);
101102

test/numbers.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,10 @@ describe('standard accounting formatting', () => {
341341
delete cldr.custom;
342342
});
343343

344+
it('should support minus zero', () => {
345+
expect(formatNumber(-0, 'a2')).toEqual("($0.00)");
346+
});
347+
344348
it('should apply format', () => {
345349
expect(formatNumber(10, 'a', 'custom')).toEqual("10.00$");
346350
});
@@ -405,6 +409,10 @@ describe('custom formatting', () => {
405409
expect(formatNumber(10.9, '#')).toEqual("11");
406410
});
407411

412+
it('should support minus zero with custom format', () => {
413+
expect(formatNumber(-0, '0.00;(0.00)')).toEqual("(0.00)");
414+
});
415+
408416
it('replaces whole part of the number', () => {
409417
expect(formatNumber(-0, '#')).toEqual("-0");
410418
});

0 commit comments

Comments
 (0)