Skip to content

A local-first personal finance app

feat(currency): Currency-influenced initial number formats (#5797)

Stephen Brown IIdc811552be2d
diff --git a/packages/desktop-client/src/components/settings/Currency.tsx b/packages/desktop-client/src/components/settings/Currency.tsx
index 8c50d49dd5..85f3b84fed 100644
--- a/packages/desktop-client/src/components/settings/Currency.tsx
+++ b/packages/desktop-client/src/components/settings/Currency.tsx
@@ -21,35 +21,36 @@ export function CurrencySettings() {
() =>
new Map<string, string>([
['', t('None')],
- ['AUD', t('Australian Dollar')],
+ ['AED', t('UAE Dirham')],
['ARS', t('Argentinian Peso')],
+ ['AUD', t('Australian Dollar')],
['BRL', t('Brazilian Real')],
['CAD', t('Canadian Dollar')],
['CHF', t('Swiss Franc')],
['CNY', t('Yuan Renminbi')],
['CRC', t('Costa Rican Colón')],
+ ['EGP', t('Egyptian Pound')],
['EUR', t('Euro')],
['GBP', t('Pound Sterling')],
['HKD', t('Hong Kong Dollar')],
['INR', t('Indian Rupee')],
['JMD', t('Jamaican Dollar')],
- // ['JPY', t('Yen')],
+ // ['JPY', t('Japanese Yen')],
['LKR', t('Sri Lankan Rupee')],
['MDL', t('Moldovan Leu')],
['PHP', t('Philippine Peso')],
['PLN', t('Polish Złoty')],
+ ['QAR', t('Qatari Riyal')],
['RON', t('Romanian Leu')],
['RSD', t('Serbian Dinar')],
['RUB', t('Russian Ruble')],
+ ['SAR', t('Saudi Riyal')],
['SEK', t('Swedish Krona')],
['SGD', t('Singapore Dollar')],
['THB', t('Thai Baht')],
['TRY', t('Turkish Lira')],
['UAH', t('Ukrainian Hryvnia')],
['USD', t('US Dollar')],
- ['QAR', t('Qatari Riyal')],
- ['EGP', t('Egyptian Pound')],
- ['SAR', t('Saudi Riyal')],
['UZS', t('Uzbek Soum')],
]),
[t],
@@ -66,6 +67,8 @@ export function CurrencySettings() {
const [spaceEnabled, setSpaceEnabledPref] = useSyncedPref(
'currencySpaceBetweenAmountAndSymbol',
);
+ const [, setNumberFormatPref] = useSyncedPref('numberFormat');
+ const [, setHideFractionPref] = useSyncedPref('hideFraction');
const selectButtonClassName = css({
'&[data-hovered]': {
@@ -87,6 +90,13 @@ export function CurrencySettings() {
const handleCurrencyChange = (code: string) => {
setDefaultCurrencyCodePref(code);
+ if (code !== '') {
+ const cur = getCurrency(code);
+ setNumberFormatPref(cur.numberFormat);
+ setHideFractionPref(cur.decimalPlaces === 0 ? 'true' : 'false');
+ setSpaceEnabledPref(cur.symbolFirst ? 'false' : 'true');
+ setSymbolPositionPref(cur.symbolFirst ? 'before' : 'after');
+ }
};
const symbolPositionOptions = useMemo(() => {
@@ -175,7 +185,9 @@ export function CurrencySettings() {
<Text>
<Trans>
<strong>Currency settings</strong> affect how amounts are displayed
- throughout the application.
+ throughout the application. Changing the currency will affect the
+ number format, symbol position, and whether fractions are shown. These
+ can be adjusted after the currency is set.
</Trans>
</Text>
</Setting>
diff --git a/packages/desktop-client/src/hooks/useFormat.ts b/packages/desktop-client/src/hooks/useFormat.ts
index b5cf72ce69..a0528bd0a7 100644
--- a/packages/desktop-client/src/hooks/useFormat.ts
+++ b/packages/desktop-client/src/hooks/useFormat.ts
@@ -72,7 +72,7 @@ function format(
// This case is generally flawed, but we need to support it for
// backwards compatibility for now.
// For example, it is not clear how the string might look like
- // The Budget sends 12300, if ther user inputs 123.00, but
+ // The Budget sends 12300, if the user inputs 123.00, but
// there might be other components that send 123 with the same user input.
// Ideally the string case will be removed in the future. We should always
// use the IntegerAmount.
@@ -149,7 +149,7 @@ export function useFormat(): UseFormatResult {
valueWithoutSign = formattedNumericValue.slice(1);
}
- const space = spaceEnabledPref === 'true' ? '\u00A0' : '';
+ const space = spaceEnabledPref === 'true' ? '\u202F' : '';
const position = symbolPositionPref || 'before';
const styledAmount =
diff --git a/packages/loot-core/src/shared/arithmetic.test.ts b/packages/loot-core/src/shared/arithmetic.test.ts
index bb3eeb07d3..f5deb030fd 100644
--- a/packages/loot-core/src/shared/arithmetic.test.ts
+++ b/packages/loot-core/src/shared/arithmetic.test.ts
@@ -43,7 +43,7 @@ describe('arithmetic', () => {
expect(evalArithmetic('1,222.45')).toEqual(1222.45);
setNumberFormat({ format: 'space-comma', hideFraction: false });
- expect(evalArithmetic('1\xa0222,45')).toEqual(1222.45);
+ expect(evalArithmetic('1\u202F222,45')).toEqual(1222.45);
setNumberFormat({ format: 'apostrophe-dot', hideFraction: false });
expect(evalArithmetic('1’222.45')).toEqual(1222.45);
diff --git a/packages/loot-core/src/shared/arithmetic.ts b/packages/loot-core/src/shared/arithmetic.ts
index 0ef3a0f74d..a028c79d41 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,.’\xa0 ]|\p{Sc}/u)) {
+ while (char(state) && char(state).match(/[0-9,.’\u00A0\u202F ]|\p{Sc}/u)) {
numberStr += next(state);
}
diff --git a/packages/loot-core/src/shared/currencies.ts b/packages/loot-core/src/shared/currencies.ts
index 762bd114e1..87b9ea83d3 100644
--- a/packages/loot-core/src/shared/currencies.ts
+++ b/packages/loot-core/src/shared/currencies.ts
@@ -1,47 +1,57 @@
+import { NumberFormats } from './util';
+
export type Currency = {
code: string;
symbol: string;
name: string;
decimalPlaces: number;
+ numberFormat: NumberFormats;
+ symbolFirst: boolean;
};
// When adding a new currency with a higher decimal precision, make sure to update
// the MAX_SAFE_NUMBER in util.ts.
// When adding a currency, also update the translation map in
-// at packages/desktop-client/src/components/settings/Currency.tsx
-// for the translation
+// packages/desktop-client/src/components/settings/Currency.tsx for the translation.
+// Number formats and symbol placement based on CLDR (Common Locale Data Repository) /
+// LDML (Locale Data Markup Language) locale conventions and Intl.NumberFormat standards
+// References:
+// https://www.localeplanet.com/icu/decimal-symbols.html
+// https://www.localeplanet.com/api/auto/currencymap.html
+// prettier-ignore
export const currencies: Currency[] = [
- { code: '', name: 'None', symbol: '', decimalPlaces: 2 },
- { code: 'AUD', name: 'Australian Dollar', symbol: 'A$', decimalPlaces: 2 },
- { code: 'ARS', name: 'Argentinian Peso', symbol: 'Arg$', decimalPlaces: 2 },
- { code: 'BRL', name: 'Brazilian Real', symbol: 'R$', decimalPlaces: 2 },
- { code: 'CAD', name: 'Canadian Dollar', symbol: 'CA$', decimalPlaces: 2 },
- { code: 'CHF', name: 'Swiss Franc', symbol: 'Fr.', decimalPlaces: 2 },
- { code: 'CNY', name: 'Yuan Renminbi', symbol: '¥', decimalPlaces: 2 },
- { code: 'CRC', name: 'Costa Rican Colón', symbol: '₡', decimalPlaces: 2 },
- { code: 'EUR', name: 'Euro', symbol: '€', decimalPlaces: 2 },
- { code: 'GBP', name: 'Pound Sterling', symbol: '£', decimalPlaces: 2 },
- { code: 'HKD', name: 'Hong Kong Dollar', symbol: 'HK$', decimalPlaces: 2 },
- { code: 'INR', name: 'Indian Rupee', symbol: '₹', decimalPlaces: 2 },
- { code: 'JMD', name: 'Jamaican Dollar', symbol: 'J$', decimalPlaces: 2 },
- // { code: 'JPY', name: 'Yen', symbol: '¥', decimalPlaces: 0 },
- { code: 'LKR', name: 'Sri Lankan Rupee', symbol: 'Rs.', decimalPlaces: 2 },
- { code: 'MDL', name: 'Moldovan Leu', symbol: 'L', decimalPlaces: 2 },
- { code: 'PHP', name: 'Philippine Peso', symbol: '₱', decimalPlaces: 2 },
- { code: 'PLN', name: 'Polish Złoty', symbol: 'zł', decimalPlaces: 2 },
- { code: 'RON', name: 'Romanian Leu', symbol: 'lei', decimalPlaces: 2 },
- { code: 'RSD', name: 'Serbian Dinar', symbol: 'RSD', decimalPlaces: 2 },
- { code: 'RUB', name: 'Russian Ruble', symbol: '₽', decimalPlaces: 2 },
- { code: 'SEK', name: 'Swedish Krona', symbol: 'kr', decimalPlaces: 2 },
- { code: 'SGD', name: 'Singapore Dollar', symbol: 'S$', decimalPlaces: 2 },
- { code: 'THB', name: 'Thai Baht', symbol: '฿', decimalPlaces: 2 },
- { code: 'TRY', name: 'Turkish Lira', symbol: '₺', decimalPlaces: 2 },
- { code: 'UAH', name: 'Ukrainian Hryvnia', symbol: '₴', decimalPlaces: 2 },
- { code: 'USD', name: 'US Dollar', symbol: '$', decimalPlaces: 2 },
- { code: 'QAR', name: 'Qatari Riyal', symbol: 'QAR', decimalPlaces: 2 },
- { code: 'EGP', name: 'Egyptian Pound', symbol: 'EGP', decimalPlaces: 2 },
- { code: 'SAR', name: 'Saudi Riyal', symbol: '﷼', decimalPlaces: 2 },
- { code: 'UZS', name: 'Uzbek Soum', symbol: 'UZS', decimalPlaces: 2 },
+ { code: '', name: 'None', symbol: '', decimalPlaces: 2, numberFormat: 'comma-dot', symbolFirst: true },
+ { code: 'AED', name: 'UAE Dirham', symbol: 'د.إ', decimalPlaces: 2, numberFormat: 'comma-dot', symbolFirst: false },
+ { code: 'ARS', name: 'Argentinian Peso', symbol: 'Arg$', decimalPlaces: 2, numberFormat: 'dot-comma', symbolFirst: true },
+ { code: 'AUD', name: 'Australian Dollar', symbol: 'A$', decimalPlaces: 2, numberFormat: 'comma-dot', symbolFirst: true },
+ { code: 'BRL', name: 'Brazilian Real', symbol: 'R$', decimalPlaces: 2, numberFormat: 'dot-comma', symbolFirst: true },
+ { code: 'CAD', name: 'Canadian Dollar', symbol: 'CA$', decimalPlaces: 2, numberFormat: 'comma-dot', symbolFirst: true },
+ { code: 'CHF', name: 'Swiss Franc', symbol: 'Fr.', decimalPlaces: 2, numberFormat: 'apostrophe-dot', symbolFirst: true },
+ { code: 'CNY', name: 'Yuan Renminbi', symbol: '¥', decimalPlaces: 2, numberFormat: 'comma-dot', symbolFirst: true },
+ { code: 'CRC', name: 'Costa Rican Colón', symbol: '₡', decimalPlaces: 2, numberFormat: 'space-comma', symbolFirst: true },
+ { code: 'EGP', name: 'Egyptian Pound', symbol: 'ج.م', decimalPlaces: 2, numberFormat: 'comma-dot', symbolFirst: false },
+ { code: 'EUR', name: 'Euro', symbol: '€', decimalPlaces: 2, numberFormat: 'dot-comma', symbolFirst: false },
+ { code: 'GBP', name: 'Pound Sterling', symbol: '£', decimalPlaces: 2, numberFormat: 'comma-dot', symbolFirst: true },
+ { code: 'HKD', name: 'Hong Kong Dollar', symbol: 'HK$', decimalPlaces: 2, numberFormat: 'comma-dot', symbolFirst: true },
+ { code: 'INR', name: 'Indian Rupee', symbol: '₹', decimalPlaces: 2, numberFormat: 'comma-dot-in', symbolFirst: true },
+ { code: 'JMD', name: 'Jamaican Dollar', symbol: 'J$', decimalPlaces: 2, numberFormat: 'comma-dot', symbolFirst: true },
+ // { code: 'JPY', name: 'Japanese Yen', symbol: '¥', decimalPlaces: 0, numberFormat: 'comma-dot', symbolFirst: true },
+ { code: 'LKR', name: 'Sri Lankan Rupee', symbol: 'Rs.', decimalPlaces: 2, numberFormat: 'comma-dot', symbolFirst: true },
+ { code: 'MDL', name: 'Moldovan Leu', symbol: 'L', decimalPlaces: 2, numberFormat: 'dot-comma', symbolFirst: false },
+ { code: 'PHP', name: 'Philippine Peso', symbol: '₱', decimalPlaces: 2, numberFormat: 'comma-dot', symbolFirst: true },
+ { code: 'PLN', name: 'Polish Złoty', symbol: 'zł', decimalPlaces: 2, numberFormat: 'space-comma', symbolFirst: false },
+ { code: 'QAR', name: 'Qatari Riyal', symbol: 'ر.ق', decimalPlaces: 2, numberFormat: 'comma-dot', symbolFirst: false },
+ { code: 'RON', name: 'Romanian Leu', symbol: 'lei', decimalPlaces: 2, numberFormat: 'dot-comma', symbolFirst: false },
+ { code: 'RSD', name: 'Serbian Dinar', symbol: 'дин', decimalPlaces: 2, numberFormat: 'dot-comma', symbolFirst: false },
+ { code: 'RUB', name: 'Russian Ruble', symbol: '₽', decimalPlaces: 2, numberFormat: 'space-comma', symbolFirst: false },
+ { code: 'SAR', name: 'Saudi Riyal', symbol: 'ر.س', decimalPlaces: 2, numberFormat: 'comma-dot', symbolFirst: false },
+ { code: 'SEK', name: 'Swedish Krona', symbol: 'kr', decimalPlaces: 2, numberFormat: 'space-comma', symbolFirst: false },
+ { code: 'SGD', name: 'Singapore Dollar', symbol: 'S$', decimalPlaces: 2, numberFormat: 'comma-dot', symbolFirst: true },
+ { code: 'THB', name: 'Thai Baht', symbol: '฿', decimalPlaces: 2, numberFormat: 'comma-dot', symbolFirst: true },
+ { code: 'TRY', name: 'Turkish Lira', symbol: '₺', decimalPlaces: 2, numberFormat: 'dot-comma', symbolFirst: true },
+ { code: 'UAH', name: 'Ukrainian Hryvnia', symbol: '₴', decimalPlaces: 2, numberFormat: 'space-comma', symbolFirst: false },
+ { code: 'USD', name: 'US Dollar', symbol: '$', decimalPlaces: 2, numberFormat: 'comma-dot', symbolFirst: true },
+ { code: 'UZS', name: 'Uzbek Soum', symbol: 'UZS', decimalPlaces: 2, numberFormat: 'space-comma', symbolFirst: false },
];
export function getCurrency(code: string): Currency {
diff --git a/packages/loot-core/src/shared/util.test.ts b/packages/loot-core/src/shared/util.test.ts
index c17f84ebb0..4eeddffa7a 100644
--- a/packages/loot-core/src/shared/util.test.ts
+++ b/packages/loot-core/src/shared/util.test.ts
@@ -97,12 +97,12 @@ describe('utility functions', () => {
test('number formatting works with space-comma format', () => {
setNumberFormat({ format: 'space-comma', 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');
+ // grouping separator space char is a narrow non-breaking space (U+202F)
+ expect(formatter.format(Number('1234.56'))).toBe('1\u202F234,56');
setNumberFormat({ format: 'space-comma', hideFraction: true });
formatter = getNumberFormat().formatter;
- expect(formatter.format(Number('1234.56'))).toBe('1\xa0235');
+ expect(formatter.format(Number('1234.56'))).toBe('1\u202F235');
});
test('number formatting works with apostrophe-dot format', () => {
diff --git a/packages/loot-core/src/shared/util.ts b/packages/loot-core/src/shared/util.ts
index 7683a17da8..a2121c3085 100644
--- a/packages/loot-core/src/shared/util.ts
+++ b/packages/loot-core/src/shared/util.ts
@@ -264,7 +264,7 @@ const NUMBER_FORMATS = [
'comma-dot-in',
] as const;
-type NumberFormats = (typeof NUMBER_FORMATS)[number];
+export type NumberFormats = (typeof NUMBER_FORMATS)[number];
function isNumberFormat(input: string = ''): input is NumberFormats {
return (NUMBER_FORMATS as readonly string[]).includes(input);
@@ -277,7 +277,11 @@ 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\xa0000,33', labelNoFraction: '1\xa0000' },
+ {
+ value: 'space-comma',
+ label: '1\u202F000,33',
+ labelNoFraction: '1\u202F000',
+ },
{ value: 'apostrophe-dot', label: '1’000.33', labelNoFraction: '1’000' },
{ value: 'comma-dot-in', label: '1,00,000.33', labelNoFraction: '1,00,000' },
];
@@ -326,8 +330,8 @@ export function getNumberFormat({
switch (format) {
case 'space-comma':
- locale = 'en-SE';
- thousandsSeparator = '\xa0';
+ locale = 'fr-FR';
+ thousandsSeparator = '\u202F';
decimalSeparator = ',';
break;
case 'dot-comma':
diff --git a/upcoming-release-notes/5797.md b/upcoming-release-notes/5797.md
new file mode 100644
index 0000000000..c602d57c05
--- /dev/null
+++ b/upcoming-release-notes/5797.md
@@ -0,0 +1,6 @@
+---
+category: Enhancements
+authors: [StephenBrown2]
+---
+
+Set initial number formats based on currency selection