Skip to content

Commit f8232b9

Browse files
Merge pull request #17 from telerik/constant-format
fix(numbers): constant format is interpreted as zero format
2 parents 3c5e269 + 055368e commit f8232b9

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/numbers/custom-number-format.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,22 @@ function roundNumber(formatOptions) {
8080
formatOptions.decimalIndex = decimalIndex;
8181
}
8282

83+
function isConstantFormat(format) {
84+
return format.indexOf(SHARP) === -1 && format.indexOf(ZERO) === -1;
85+
}
86+
8387
function setValueSpecificFormat(formatOptions) {
8488
let { number, format } = formatOptions;
8589
format = format.split(";");
8690
if (formatOptions.negative && format[1]) {
8791
format = format[1];
8892
formatOptions.hasNegativeFormat = true;
8993
} else if (number === 0) {
90-
format = format[2] || format[0];
94+
const zeroFormat = format[2];
95+
format = zeroFormat || format[0];
96+
if (zeroFormat && isConstantFormat(zeroFormat)) {
97+
formatOptions.constant = zeroFormat;
98+
}
9199
} else {
92100
format = format[0];
93101
}
@@ -246,10 +254,6 @@ function applyCustomFormat(formatOptions, info) {
246254
return number;
247255
}
248256

249-
function isConstantFormat(format) {
250-
return format.indexOf(SHARP) === -1 && format.indexOf(ZERO) === -1;
251-
}
252-
253257
export default function customNumberFormat(number, format, info) {
254258
const formatOptions = {
255259
negative: number < 0,
@@ -259,8 +263,8 @@ export default function customNumberFormat(number, format, info) {
259263

260264
setValueSpecificFormat(formatOptions);
261265

262-
if (isConstantFormat(formatOptions.format)) {
263-
return formatOptions.format;
266+
if (formatOptions.constant) {
267+
return formatOptions.constant;
264268
}
265269

266270
setFormatLiterals(formatOptions);

test/numbers.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,10 @@ describe('custom formatting', () => {
482482
expect(formatNumber(17.115, "#.##")).toEqual("17.12");
483483
});
484484

485+
it("returns number if format is a constant", () => {
486+
expect(formatNumber(0, "Foo")).toEqual("0");
487+
});
488+
485489
});
486490

487491
describe('parseNumber', () => {

0 commit comments

Comments
 (0)