Skip to content

Commit

Permalink
fix: format currency to number (#250)
Browse files Browse the repository at this point in the history
* fix: format currency to number

* feat: created tests to currency to number

Co-authored-by: Michael Amaral <[email protected]>
  • Loading branch information
marcelxsilva and ammichael authored Dec 28, 2020
1 parent e1dc82c commit d40c62b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/__tests__/currencyToNumber.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
2 changes: 1 addition & 1 deletion src/currencyToNumber.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const currencyToNumber = (currency: string): number =>
parseFloat(currency.replace('R$', '').replace('.', '').replace(',', '.'));
parseFloat(currency.replace('R$', '').replace(/\./g, '').replace(/,/g, '.'))

0 comments on commit d40c62b

Please sign in to comment.