From d40c62bad81cfd2c5243736a80c6cafb26284b65 Mon Sep 17 00:00:00 2001 From: Marcelo Silva Date: Mon, 28 Dec 2020 14:01:12 -0300 Subject: [PATCH] fix: format currency to number (#250) * fix: format currency to number * feat: created tests to currency to number Co-authored-by: Michael Amaral --- src/__tests__/currencyToNumber.spec.ts | 8 ++++++++ src/currencyToNumber.ts | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/__tests__/currencyToNumber.spec.ts b/src/__tests__/currencyToNumber.spec.ts index 8f9b2cc..7330720 100644 --- a/src/__tests__/currencyToNumber.spec.ts +++ b/src/__tests__/currencyToNumber.spec.ts @@ -9,4 +9,12 @@ describe('currencyToNumber tests', () => { const parsed = currencyToNumber('R$ 1000,10'); expect(parsed).toBe(1000.1); }); + it('must analyze for millions', () => { + const parsed = currencyToNumber('R$ 1.000.000,00'); + expect(parsed).toBe(1000000); + }); + it('must analyze for millions with decimals', () => { + const parsed = currencyToNumber('R$ 1.000.000,50'); + expect(parsed).toBe(1000000.5); + }); }); diff --git a/src/currencyToNumber.ts b/src/currencyToNumber.ts index 24e6a8e..a244761 100644 --- a/src/currencyToNumber.ts +++ b/src/currencyToNumber.ts @@ -1,2 +1,2 @@ export const currencyToNumber = (currency: string): number => - parseFloat(currency.replace('R$', '').replace('.', '').replace(',', '.')); +parseFloat(currency.replace('R$', '').replace(/\./g, '').replace(/,/g, '.'))