Skip to content
Snippets Groups Projects
Unverified Commit 2959054d authored by Sreetam Das's avatar Sreetam Das Committed by GitHub
Browse files

Update regex for `looselyParseAmount` for 5-9 decimal places (#2799)

* Update regex for `looselyParseAmount` for 5-9 decimal places

* Add release note

* Update comment, add some more tests
parent 7d960579
No related branches found
No related tags found
No related merge requests found
...@@ -2,7 +2,7 @@ import { looselyParseAmount, getNumberFormat, setNumberFormat } from './util'; ...@@ -2,7 +2,7 @@ import { looselyParseAmount, getNumberFormat, setNumberFormat } from './util';
describe('utility functions', () => { describe('utility functions', () => {
test('looseParseAmount works with basic numbers', () => { test('looseParseAmount works with basic numbers', () => {
// Parsing is currently limited to 1,2 decimal places or 5. // Parsing is currently limited to 1,2 decimal places or 5-9.
// Ignoring 3 places removes the possibility of improper parse // Ignoring 3 places removes the possibility of improper parse
// of amounts without decimal amounts included. // of amounts without decimal amounts included.
expect(looselyParseAmount('3')).toBe(3); expect(looselyParseAmount('3')).toBe(3);
...@@ -10,12 +10,20 @@ describe('utility functions', () => { ...@@ -10,12 +10,20 @@ describe('utility functions', () => {
expect(looselyParseAmount('3.45')).toBe(3.45); expect(looselyParseAmount('3.45')).toBe(3.45);
expect(looselyParseAmount('3.456')).toBe(3456); expect(looselyParseAmount('3.456')).toBe(3456);
expect(looselyParseAmount('3.45000')).toBe(3.45); expect(looselyParseAmount('3.45000')).toBe(3.45);
expect(looselyParseAmount('3.450000')).toBe(3.45);
expect(looselyParseAmount('3.4500000')).toBe(3.45);
expect(looselyParseAmount('3.45000000')).toBe(3.45);
expect(looselyParseAmount('3.450000000')).toBe(3.45);
}); });
test('looseParseAmount works with alternate formats', () => { test('looseParseAmount works with alternate formats', () => {
expect(looselyParseAmount('3,45')).toBe(3.45); expect(looselyParseAmount('3,45')).toBe(3.45);
expect(looselyParseAmount('3,456')).toBe(3456); expect(looselyParseAmount('3,456')).toBe(3456);
expect(looselyParseAmount('3,45000')).toBe(3.45); expect(looselyParseAmount('3,45000')).toBe(3.45);
expect(looselyParseAmount('3,450000')).toBe(3.45);
expect(looselyParseAmount('3,4500000')).toBe(3.45);
expect(looselyParseAmount('3,45000000')).toBe(3.45);
expect(looselyParseAmount('3,450000000')).toBe(3.45);
}); });
test('looseParseAmount works with negative numbers', () => { test('looseParseAmount works with negative numbers', () => {
......
...@@ -409,9 +409,9 @@ export function looselyParseAmount(amount: string) { ...@@ -409,9 +409,9 @@ export function looselyParseAmount(amount: string) {
amount = amount.replace('(', '-').replace(')', ''); amount = amount.replace('(', '-').replace(')', '');
} }
//look for a decimal marker, then look for either 5 or 1-2 decimal places. // Look for a decimal marker, then look for either 1-2 or 5-9 decimal places.
// This avoids matching against 3 places which may not actually be decimal // This avoids matching against 3 places which may not actually be decimal
const m = amount.match(/[.,]([^.,]{5}|[^.,]{1,2})$/); const m = amount.match(/[.,]([^.,]{5,9}|[^.,]{1,2})$/);
if (!m || m.index === undefined || m.index === 0) { if (!m || m.index === undefined || m.index === 0) {
return safeNumber(parseFloat(extractNumbers(amount))); return safeNumber(parseFloat(extractNumbers(amount)));
} }
......
---
category: Bugfix
authors: [sreetamdas]
---
Fix amount parsing with 6-9 decimal places
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment