Skip to content

Commit 850fd4b

Browse files
committed
[FIX] format: number format with no digit
Since e8b38a a format with no date part and no digit is (correctly) detected as a number format. But the implementation of a format with no digit applied to a non-zero number was not correct. `123` formatted with `[$str]` was formatted as `str123` instead of `str`. closes #8395 Task: 6068824 X-original-commit: c438eb3 Signed-off-by: Lucas Lefèvre (lul) <lul@odoo.com> Signed-off-by: Adrien Minne (adrm) <adrm@odoo.com>
1 parent de5d244 commit 850fd4b

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

src/helpers/format/format.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,12 @@ function applyInternalNumberFormat(value: number, format: NumberInternalFormat,
251251
}
252252
const { integerDigits, decimalDigits } = splitNumber(Math.abs(value), maxDecimals);
253253

254+
const hasDigitInDecimalPart = !!format.decimalPart?.some((token) => token.type === "DIGIT");
254255
let formattedValue = applyIntegerFormat(
255256
integerDigits,
256257
format,
257-
format.thousandsSeparator ? locale.thousandsSeparator : undefined
258+
format.thousandsSeparator ? locale.thousandsSeparator : undefined,
259+
hasDigitInDecimalPart
258260
);
259261

260262
if (format.decimalPart !== undefined) {
@@ -273,10 +275,12 @@ function applyInternalNumberFormat(value: number, format: NumberInternalFormat,
273275
function applyIntegerFormat(
274276
integerDigits: string,
275277
internalFormat: NumberInternalFormat,
276-
thousandsSeparator: string | undefined
278+
thousandsSeparator: string | undefined,
279+
hasDigitInDecimalPart: boolean
277280
): string {
278281
let tokens = internalFormat.integerPart;
279-
if (!tokens.some((token) => token.type === "DIGIT")) {
282+
// If the format has a decimal part with digits, we always want to display the integer digits
283+
if (hasDigitInDecimalPart && !tokens.some((token) => token.type === "DIGIT")) {
280284
tokens = [...tokens, { type: "DIGIT", value: "#" }];
281285
}
282286
if (integerDigits === "0") {

tests/formats/format_helpers.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,14 @@ describe("formatValue on number", () => {
409409
expect(() => formatValue(1234, { format: "# @", locale })).toThrow();
410410
expect(() => formatValue(1234, { format: "@0", locale })).toThrow();
411411
});
412+
413+
test("Format with no digit in them", () => {
414+
expect(formatValue(200, { format: "[$Positive]", locale })).toBe("Positive");
415+
expect(formatValue(-123, { format: '0;,"Negative"', locale })).toBe("Negative");
416+
expect(formatValue(0, { format: '0;0;"Zero"', locale })).toBe("Zero");
417+
expect(formatValue("test", { format: '"Smile"', locale })).toBe("test");
418+
expect(formatValue("test", { format: '@"Smile"', locale })).toBe("testSmile");
419+
});
412420
});
413421

414422
describe("formatValue on large numbers", () => {

0 commit comments

Comments
 (0)