A local-first personal finance app
Allow 5 decimal places in file import parser (#2588)
youngcw291e3a8d14bc
diff --git a/packages/loot-core/src/shared/util.test.ts b/packages/loot-core/src/shared/util.test.tsindex eaee1ee844..d236a6652a 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 agnosticexpect(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.tsindex 727aa8eeb4..2a8d0e8cde 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.mdnew file mode 100644index 0000000000..8552dc63e2--- /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