diff --git a/src/utils/money.spec.ts b/src/utils/money.spec.ts index 25ead28..933efd6 100644 --- a/src/utils/money.spec.ts +++ b/src/utils/money.spec.ts @@ -17,6 +17,12 @@ describe("Function: convertStringCurrencyToNumber", () => { it("handles additional decimals", () => { expect(convertStringCurrencyToNumber("100.011")).toEqual(100.01); }); + it("handles parentheses", () => { + expect(convertStringCurrencyToNumber("($10.33)")).toEqual(-10.33); + }); + it("handles all the things at once", () => { + expect(convertStringCurrencyToNumber(" ($2,334.932) ")).toEqual(-2334.93); + }); }); describe("Function: roundCurrency", () => { diff --git a/src/utils/money.ts b/src/utils/money.ts index d81410a..a4e0c5a 100644 --- a/src/utils/money.ts +++ b/src/utils/money.ts @@ -3,8 +3,11 @@ // export const convertStringCurrencyToNumber = (amountRaw: string): number => { - amountRaw = amountRaw.trim().replaceAll("$", "").replaceAll(",", ""); - const amountParsed = parseFloat(amountRaw); + let amountInProcess = amountRaw.trim().replaceAll("$", "").replaceAll(",", ""); + if (amountInProcess.match(/^\(\d*\.?\d*\)$/)) { + amountInProcess = amountInProcess.replace("(", "-").replace(")", ""); + } + const amountParsed = parseFloat(amountInProcess); return roundCurrency(amountParsed); };