Fix cut numbers (#3072)

This commit is contained in:
poulch 2023-01-30 14:49:08 +01:00 committed by GitHub
parent 6dbb8e4ecc
commit 83962d6e93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 5 deletions

View file

@ -64,4 +64,16 @@ describe("parseCurrency", () => {
// Assert
expect(parsed).toBe(2.07);
});
it("should not trim price without decimal", () => {
// Arrange
const value = "2123";
const currency = "EUR";
const locale = Locale.PL;
// Act
const parsed = parseCurrency(value, locale, currency);
// Assert
expect(parsed).toBe(2123);
});
});

View file

@ -26,12 +26,10 @@ export const parseCurrency = (
// Thousand seperators are not allowedd
const number = value.replace(/,/, ".");
const fractionDigits = getFractionDigits(locale, currency);
const trimmedNumber = number.slice(
0,
number.lastIndexOf(".") + 1 + fractionDigits,
);
const lastDecimalPoint = number.lastIndexOf(".");
const trimmedNumber = number.slice(0, lastDecimalPoint + 1 + fractionDigits);
return parseFloat(trimmedNumber);
return parseFloat(lastDecimalPoint !== -1 ? trimmedNumber : number);
};
export const prepareVariantChangeData = (