Skip to content

A local-first personal finance app

Fix number formatting with non-breaking space (#2981)

Julian Wachholzec3475d8346d
diff --git a/packages/loot-core/src/shared/arithmetic.test.ts b/packages/loot-core/src/shared/arithmetic.test.ts
index 0475689b8d..b897545dcd 100644
--- a/packages/loot-core/src/shared/arithmetic.test.ts
+++ b/packages/loot-core/src/shared/arithmetic.test.ts
@@ -1,4 +1,5 @@
import { evalArithmetic } from './arithmetic';
+import { setNumberFormat } from './util';
describe('arithmetic', () => {
test('handles negative numbers', () => {
@@ -40,5 +41,8 @@ describe('arithmetic', () => {
test('respects current number format', () => {
expect(evalArithmetic('1,222.45')).toEqual(1222.45);
+
+ setNumberFormat({ format: 'space-comma', hideFraction: false });
+ expect(evalArithmetic('1\xa0222,45')).toEqual(1222.45);
});
});
diff --git a/packages/loot-core/src/shared/arithmetic.ts b/packages/loot-core/src/shared/arithmetic.ts
index f69543b1d5..bd4d26df82 100644
--- a/packages/loot-core/src/shared/arithmetic.ts
+++ b/packages/loot-core/src/shared/arithmetic.ts
@@ -38,7 +38,7 @@ function parsePrimary(state) {
}
let numberStr = '';
- while (char(state) && char(state).match(/[0-9,. ]|\p{Sc}/u)) {
+ while (char(state) && char(state).match(/[0-9,.\xa0 ]|\p{Sc}/u)) {
numberStr += next(state);
}
diff --git a/packages/loot-core/src/shared/util.test.ts b/packages/loot-core/src/shared/util.test.ts
index 6d1945b298..ac2f2b70a1 100644
--- a/packages/loot-core/src/shared/util.test.ts
+++ b/packages/loot-core/src/shared/util.test.ts
@@ -84,15 +84,4 @@ describe('utility functions', () => {
formatter = getNumberFormat().formatter;
expect(formatter.format(Number('1234.56'))).toBe('1\xa0235');
});
-
- test('number formatting works with space-dot format', () => {
- setNumberFormat({ format: 'space-dot', hideFraction: false });
- let formatter = getNumberFormat().formatter;
- // grouping separator space char is a non-breaking space, or UTF-16 \xa0
- expect(formatter.format(Number('1234.56'))).toBe('1\xa0234.56');
-
- setNumberFormat({ format: 'space-dot', hideFraction: true });
- formatter = getNumberFormat().formatter;
- expect(formatter.format(Number('1234.56'))).toBe('1\xa0235');
- });
});
diff --git a/packages/loot-core/src/shared/util.ts b/packages/loot-core/src/shared/util.ts
index 4602177457..cac3ff0bb5 100644
--- a/packages/loot-core/src/shared/util.ts
+++ b/packages/loot-core/src/shared/util.ts
@@ -217,12 +217,7 @@ export function appendDecimals(
return amountToCurrency(currencyToAmount(result));
}
-type NumberFormats =
- | 'comma-dot'
- | 'dot-comma'
- | 'space-comma'
- | 'space-dot'
- | 'comma-dot-in';
+type NumberFormats = 'comma-dot' | 'dot-comma' | 'space-comma' | 'comma-dot-in';
export const numberFormats: Array<{
value: NumberFormats;
@@ -231,8 +226,7 @@ export const numberFormats: Array<{
}> = [
{ value: 'comma-dot', label: '1,000.33', labelNoFraction: '1,000' },
{ value: 'dot-comma', label: '1.000,33', labelNoFraction: '1.000' },
- { value: 'space-comma', label: '1 000,33', labelNoFraction: '1 000' },
- { value: 'space-dot', label: '1 000.33', labelNoFraction: '1 000' },
+ { value: 'space-comma', label: '1\xa0000,33', labelNoFraction: '1\xa0000' },
{ value: 'comma-dot-in', label: '1,00,000.33', labelNoFraction: '1,00,000' },
];
@@ -269,12 +263,6 @@ export function getNumberFormat({
regex = /[^-0-9,]/g;
separator = ',';
break;
- case 'space-dot':
- locale = 'dje';
- regex = /[^-0-9,.]/g;
- separator = '.';
- separatorRegex = /[,.]/g;
- break;
case 'comma-dot-in':
locale = 'en-IN';
regex = /[^-0-9.]/g;
diff --git a/upcoming-release-notes/2981.md b/upcoming-release-notes/2981.md
new file mode 100644
index 0000000000..6c1a2fb48b
--- /dev/null
+++ b/upcoming-release-notes/2981.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [julianwachholz]
+---
+
+Fix number parsing with and remove unsupported "space-dot" format