Skip to content

Commit 82562a6

Browse files
fix(formatNumber): remove trailing zeros after rounding
1 parent 45de3bc commit 82562a6

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

src/numbers/custom-number-format.js

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const ZERO = "0";
1414
const EMPTY = "";
1515

1616
const literalRegExp = /(\\.)|(['][^']*[']?)|(["][^"]*["]?)/g;
17+
const trailingZerosRegExp = /(\.(?:[0-9]*[1-9])?)0+$/g;
1718
const commaRegExp = /\,/g;
1819

1920
function setFormatLiterals(formatOptions) {
@@ -31,6 +32,21 @@ function setFormatLiterals(formatOptions) {
3132
}
3233
}
3334

35+
function trimTrailingZeros(value, lastZero) {
36+
let result;
37+
if (lastZero === 0) {
38+
result = value.replace(trailingZerosRegExp, '$1');
39+
} else {
40+
result = value.replace(new RegExp(`(\\.[0-9]{${ lastZero }}[1-9]*)0+$`, 'g'), '$1');
41+
}
42+
43+
if (result.charAt(result.length - 1) === POINT) {
44+
result = result.substr(0, result.length - 1);
45+
}
46+
47+
return result;
48+
}
49+
3450
function roundNumber(formatOptions) {
3551
let { number, format } = formatOptions;
3652
let decimalIndex = format.indexOf(POINT);
@@ -49,24 +65,30 @@ function roundNumber(formatOptions) {
4965
}
5066
fraction = fraction.split(POINT)[1] || EMPTY;
5167

52-
let idx = fraction.length;
68+
let precision = fraction.length;
69+
let trailingZeros = -1;
5370

5471
if (!hasZero && !hasSharp) {
5572
formatOptions.format = format.substring(0, decimalIndex) + format.substring(decimalIndex + 1);
5673
decimalIndex = -1;
57-
idx = 0;
74+
precision = 0;
5875
} else if (hasZero && zeroIndex > sharpIndex) {
59-
idx = zeroIndex;
76+
precision = zeroIndex;
6077
} else if (sharpIndex > zeroIndex) {
61-
if (hasSharp && idx > sharpIndex) {
62-
idx = sharpIndex;
63-
} else if (hasZero && idx < zeroIndex) {
64-
idx = zeroIndex;
78+
if (hasSharp && precision > sharpIndex) {
79+
precision = sharpIndex;
80+
} else if (hasZero && precision < zeroIndex) {
81+
precision = zeroIndex;
6582
}
83+
84+
trailingZeros = hasZero ? zeroIndex : 0;
6685
}
6786

68-
if (idx > -1) {
69-
number = round(number, idx);
87+
if (precision > -1) {
88+
number = round(number, precision);
89+
if (trailingZeros > -1) {
90+
number = trimTrailingZeros(number, trailingZeros);
91+
}
7092
}
7193
} else {
7294
number = round(number);

test/numbers.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,13 @@ describe('custom formatting', () => {
447447
expect(formatNumber(3.235555, "0.#0")).toEqual("3.24");
448448
});
449449

450+
it("removes trailing zeros after rounding", () => {
451+
expect(formatNumber(0.016999999999, "#.#####")).toEqual("0.017");
452+
expect(formatNumber(0.016999999999, "#.0000#")).toEqual("0.0170");
453+
expect(formatNumber(1.999, "0.0#")).toEqual("2.0");
454+
expect(formatNumber(1.999, "0.#")).toEqual("2");
455+
});
456+
450457
it("removes decimal part if no number placeholder", () => {
451458
expect(formatNumber(3.222, "0.")).toEqual("3");
452459
});
@@ -460,7 +467,7 @@ describe('custom formatting', () => {
460467
});
461468

462469
it("applies negative format rounding", () => {
463-
expect(formatNumber(-0.001, "####;-(#.#)")).toEqual("-(0.0)");
470+
expect(formatNumber(-0.001, "####;-(#.#)")).toEqual("-(0)");
464471
});
465472

466473
it("toString decimal number -1000 with negative format", () => {
@@ -472,7 +479,7 @@ describe('custom formatting', () => {
472479
});
473480

474481
it("clears negative sign if rounded number is positive", () => {
475-
expect(formatNumber(-0.00001, "#.##")).toEqual("0.00");
482+
expect(formatNumber(-0.00001, "#.##")).toEqual("0");
476483
});
477484

478485
it("formats 0", () => {

0 commit comments

Comments
 (0)