Skip to content
Snippets Groups Projects
Unverified Commit 291e3a8d authored by youngcw's avatar youngcw Committed by GitHub
Browse files

Allow 5 decimal places in file import parser (#2588)

* allow specifically 5 decimal places in parser

* note

* update tests

* lint
parent edd34b79
No related branches found
No related tags found
No related merge requests found
...@@ -2,19 +2,20 @@ import { looselyParseAmount, getNumberFormat, setNumberFormat } from './util'; ...@@ -2,19 +2,20 @@ 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.
// Ignoring 3 places removes the possibility of improper parse
// of amounts without decimal amounts included.
expect(looselyParseAmount('3')).toBe(3); expect(looselyParseAmount('3')).toBe(3);
expect(looselyParseAmount('3.4')).toBe(3.4);
expect(looselyParseAmount('3.45')).toBe(3.45); expect(looselyParseAmount('3.45')).toBe(3.45);
// Parsing is currently limited to 2 decimal places.
// Only <. ,> and 2 other chars counts as decimal places
// Proper parsing would include format settings,
// but currently we are format agnostic
expect(looselyParseAmount('3.456')).toBe(3456); expect(looselyParseAmount('3.456')).toBe(3456);
expect(looselyParseAmount('3.45000')).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);
}); });
test('looseParseAmount works with negative numbers', () => { test('looseParseAmount works with negative numbers', () => {
......
...@@ -379,7 +379,9 @@ export function looselyParseAmount(amount: string) { ...@@ -379,7 +379,9 @@ export function looselyParseAmount(amount: string) {
amount = amount.replace('(', '-').replace(')', ''); amount = amount.replace('(', '-').replace(')', '');
} }
const m = amount.match(/[.,][^.,]{1,2}$/); //look for a decimal marker, then look for either 5 or 1-2 decimal places.
// This avoids matching against 3 places which may not actually be decimal
const m = amount.match(/[.,]([^.,]{5}|[^.,]{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: [youngcw]
---
Allow 5 decimal places in csv files without matching on 3 or 4
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