diff --git a/packages/loot-core/src/shared/util.test.ts b/packages/loot-core/src/shared/util.test.ts index eaee1ee844e4afbbf709f58c5b6463ea86ed7949..d236a6652a826306e128bbfaca33d18634f07601 100644 --- a/packages/loot-core/src/shared/util.test.ts +++ b/packages/loot-core/src/shared/util.test.ts @@ -2,19 +2,20 @@ import { looselyParseAmount, getNumberFormat, setNumberFormat } from './util'; describe('utility functions', () => { 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.4')).toBe(3.4); 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.45000')).toBe(3.45); }); test('looseParseAmount works with alternate formats', () => { expect(looselyParseAmount('3,45')).toBe(3.45); expect(looselyParseAmount('3,456')).toBe(3456); + expect(looselyParseAmount('3,45000')).toBe(3.45); }); test('looseParseAmount works with negative numbers', () => { diff --git a/packages/loot-core/src/shared/util.ts b/packages/loot-core/src/shared/util.ts index 727aa8eeb40f94361be901b5eea41b1205659a91..2a8d0e8cde1d47d62071b3647070ea781d7d6098 100644 --- a/packages/loot-core/src/shared/util.ts +++ b/packages/loot-core/src/shared/util.ts @@ -379,7 +379,9 @@ export function looselyParseAmount(amount: string) { 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) { return safeNumber(parseFloat(extractNumbers(amount))); } diff --git a/upcoming-release-notes/2588.md b/upcoming-release-notes/2588.md new file mode 100644 index 0000000000000000000000000000000000000000..8552dc63e27dd49095a10823fcfe1d011a2be4ca --- /dev/null +++ b/upcoming-release-notes/2588.md @@ -0,0 +1,6 @@ +--- +category: Bugfix +authors: [youngcw] +--- + +Allow 5 decimal places in csv files without matching on 3 or 4