A local-first personal finance app
fix: keep the decimal point of imported amounts that have a currency suffix (#9100)
Mohammed Yasin Zuhayr3ea2574344f5
diff --git a/packages/loot-core/src/shared/util.test.ts b/packages/loot-core/src/shared/util.test.tsindex a556c21b6b..9d2953cd4a 100644--- a/packages/loot-core/src/shared/util.test.ts+++ b/packages/loot-core/src/shared/util.test.ts@@ -83,6 +83,21 @@ describe('utility functions', () => {expect(looselyParseAmount(' 3.45 ')).toBe(3.45);});+ test('looseParseAmount does not count a currency suffix as decimal places', () => {+ expect(looselyParseAmount('3.4 €')).toBe(3.4);+ expect(looselyParseAmount('3.45€')).toBe(3.45);+ expect(looselyParseAmount('3.45 €')).toBe(3.45);+ expect(looselyParseAmount('3,45 €')).toBe(3.45);+ expect(looselyParseAmount('3.45 USD')).toBe(3.45);+ expect(looselyParseAmount('3.45000000 €')).toBe(3.45);+ expect(looselyParseAmount(' -110.7 €')).toBe(-110.7);+ expect(looselyParseAmount('1.234,5 kr')).toBe(1234.5);+ expect(looselyParseAmount('(3.4 €)')).toBe(-3.4);+ // three places are still ambiguous, same as without a suffix+ expect(looselyParseAmount('3.456 €')).toBe(3456);+ expect(looselyParseAmount('1.500 €')).toBe(1500);+ });+test('number formatting works with comma-dot format', () => {setNumberFormat({ format: 'comma-dot', hideFraction: false });let formatter = getNumberFormat().formatter;diff --git a/packages/loot-core/src/shared/util.ts b/packages/loot-core/src/shared/util.tsindex fb2b1bf843..57207be61e 100644--- a/packages/loot-core/src/shared/util.ts+++ b/packages/loot-core/src/shared/util.ts@@ -588,8 +588,10 @@ export function looselyParseAmount(amount: string) {}// Look for a decimal marker, then look for either 1-2 or 4-9 decimal places.- // This avoids matching against 3 places which may not actually be decimal- const m = amount.match(/[.,]([^.,]{4,9}|[^.,]{1,2})$/);+ // This avoids matching against 3 places which may not actually be decimal.+ // Only digits count as places, so a currency symbol or code after the+ // number does not change how many there are.+ const m = amount.match(/[.,](\d{4,9}|\d{1,2})[^\d.,]*$/);if (!m || m.index === undefined) {return safeNumber(parseFloat(extractNumbers(amount)));}diff --git a/upcoming-release-notes/import-amount-currency-suffix.md b/upcoming-release-notes/import-amount-currency-suffix.mdnew file mode 100644index 0000000000..d23c620f3f--- /dev/null+++ b/upcoming-release-notes/import-amount-currency-suffix.md@@ -0,0 +1,6 @@+---+category: Bugfix+authors: [YasinzHyper]+---++Fix imported amounts being read without their decimal point when a currency symbol or code follows the number