Update csv amount parser. Fix #2374 (#2399)

* specify 2 decimal places in the csv amount parser

* note

* update tests

* cleanup
This commit is contained in:
youngcw
2024-02-28 11:39:23 -07:00
committed by GitHub
parent 52f1f79c01
commit 6d7d12138c
3 changed files with 13 additions and 6 deletions

View File

@@ -5,15 +5,16 @@ describe('utility functions', () => {
expect(looselyParseAmount('3')).toBe(3);
expect(looselyParseAmount('3.45')).toBe(3.45);
// Right now it doesn't actually parse an "amount", it just parses
// a number. An "amount" is a valid transaction amount, usually a
// number with 2 decimal places.
expect(looselyParseAmount('3.456')).toBe(3.456);
// 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);
});
test('looseParseAmount works with alternate formats', () => {
expect(looselyParseAmount('3,45')).toBe(3.45);
expect(looselyParseAmount('3,456')).toBe(3.456);
expect(looselyParseAmount('3,456')).toBe(3456);
});
test('looseParseAmount works with negative numbers', () => {

View File

@@ -370,7 +370,7 @@ export function looselyParseAmount(amount: string) {
amount = amount.replace('(', '-').replace(')', '');
}
const m = amount.match(/[.,][^.,]*$/);
const m = amount.match(/[.,][^.,]{1,2}$/);
if (!m || m.index === undefined || m.index === 0) {
return safeNumber(parseFloat(extractNumbers(amount)));
}

View File

@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [youngcw]
---
Only match 2 decimal places when parsing amounts for file import