fix(payment-stripe): fix rounding (#8753)

This commit is contained in:
Carlos R. L. Rodrigues
2024-08-25 10:44:51 +02:00
committed by GitHub
parent 531285c1e9
commit 12670683af
2 changed files with 10 additions and 3 deletions
@@ -9,10 +9,13 @@ describe("getSmallestUnit", () => {
expect(getSmallestUnit(5.124, "KWD")).toBe(5130)
// 2 decimals
expect(getSmallestUnit(2.675, "USD")).toBe(268)
expect(getSmallestUnit(100.54, "USD")).toBe(10054)
expect(getSmallestUnit(5.126, "KWD")).toBe(5130)
expect(getSmallestUnit(0.54, "USD")).toBe(54)
expect(getSmallestUnit(0.054, "USD")).toBe(5)
expect(getSmallestUnit(0.005104, "USD")).toBe(0)
expect(getSmallestUnit(0.005104, "USD")).toBe(1)
expect(getSmallestUnit(0.004104, "USD")).toBe(0)
})
})
@@ -47,10 +47,14 @@ export function getSmallestUnit(
currency: string
): number {
const multiplier = getCurrencyMultiplier(currency)
const smallestAmount = new BigNumber(MathBN.mult(amount, multiplier))
let amount_ =
Math.round(new BigNumber(MathBN.mult(amount, multiplier)).numeric) /
multiplier
const smallestAmount = new BigNumber(MathBN.mult(amount_, multiplier))
let numeric = smallestAmount.numeric
// Check if the currency requires rounding to the nearest ten
if (multiplier === 1e3) {
numeric = Math.ceil(numeric / 10) * 10