Skip to content

A local-first personal finance app

[AI] feat(currencies): add currency-precision-aware helpers (#8064)

Stephen Brown II23da0016d2a1
diff --git a/packages/loot-core/src/shared/currencies.test.ts b/packages/loot-core/src/shared/currencies.test.ts
new file mode 100644
index 0000000000..b4c934bdfc
--- /dev/null
+++ b/packages/loot-core/src/shared/currencies.test.ts
@@ -0,0 +1,44 @@
+import { currencies, getDecimalPlaces } from './currencies';
+
+describe('getDecimalPlaces', () => {
+ it('returns 2 for empty string (None)', () => {
+ expect(getDecimalPlaces('')).toBe(2);
+ });
+
+ it('returns 0 for zero-decimal currencies', () => {
+ expect(getDecimalPlaces('JPY')).toBe(0);
+ expect(getDecimalPlaces('KRW')).toBe(0);
+ expect(getDecimalPlaces('IRR')).toBe(0);
+ });
+
+ it('returns 2 for standard currencies', () => {
+ expect(getDecimalPlaces('USD')).toBe(2);
+ expect(getDecimalPlaces('EUR')).toBe(2);
+ expect(getDecimalPlaces('GBP')).toBe(2);
+ });
+
+ it('returns 2 as safe default for unknown codes', () => {
+ expect(getDecimalPlaces('XYZ')).toBe(2);
+ });
+});
+
+describe('currency metadata', () => {
+ it.each(currencies)(
+ '$name ($code) has a numeric decimalPlaces field',
+ currency => {
+ expect(typeof currency.decimalPlaces).toBe('number');
+ },
+ );
+
+ it.each(currencies)(
+ '$name ($code) has non-negative decimalPlaces',
+ currency => {
+ expect(currency.decimalPlaces).toBeGreaterThanOrEqual(0);
+ },
+ );
+
+ it('first entry is None with code empty string and decimalPlaces 2', () => {
+ expect(currencies[0].code).toBe('');
+ expect(currencies[0].decimalPlaces).toBe(2);
+ });
+});
diff --git a/packages/loot-core/src/shared/currencies.ts b/packages/loot-core/src/shared/currencies.ts
index 3a82411682..50c2752b00 100644
--- a/packages/loot-core/src/shared/currencies.ts
+++ b/packages/loot-core/src/shared/currencies.ts
@@ -72,3 +72,7 @@ export const currencies: Currency[] = [
export function getCurrency(code: string): Currency {
return currencies.find(c => c.code === code) || currencies[0];
}
+
+export function getDecimalPlaces(currencyCode: string): number {
+ return getCurrency(currencyCode)?.decimalPlaces ?? 2;
+}
diff --git a/packages/loot-core/src/shared/util.test.ts b/packages/loot-core/src/shared/util.test.ts
index db9406d0e2..a556c21b6b 100644
--- a/packages/loot-core/src/shared/util.test.ts
+++ b/packages/loot-core/src/shared/util.test.ts
@@ -1,6 +1,8 @@
import {
+ amountToCurrencyInteger,
currencyToAmount,
getNumberFormat,
+ integerToCurrencyWithDecimal,
looselyParseAmount,
setNumberFormat,
stringToInteger,
@@ -237,4 +239,69 @@ describe('utility functions', () => {
// Unicode minus
expect(stringToInteger('−3')).toBe(-3);
});
+
+ describe('integerToCurrencyWithDecimal with currency', () => {
+ beforeEach(() => {
+ setNumberFormat({ format: 'comma-dot', hideFraction: false });
+ });
+
+ test('empty string (None) uses 2dp', () => {
+ expect(integerToCurrencyWithDecimal(1234, '')).toBe('12.34');
+ });
+
+ test('USD uses 2dp', () => {
+ expect(integerToCurrencyWithDecimal(1234, 'USD')).toBe('12.34');
+ });
+
+ test('JPY uses 0dp', () => {
+ expect(integerToCurrencyWithDecimal(1234, 'JPY')).toBe('1,234');
+ });
+
+ test('IRR uses 0dp', () => {
+ expect(integerToCurrencyWithDecimal(1234, 'IRR')).toBe('1,234');
+ });
+
+ test('unknown currency XYZ defaults to 2dp', () => {
+ expect(integerToCurrencyWithDecimal(1234, 'XYZ')).toBe('12.34');
+ });
+
+ test('no currency argument preserves existing behavior', () => {
+ expect(integerToCurrencyWithDecimal(1234)).toBe('12.34');
+ expect(integerToCurrencyWithDecimal(1200)).toBe('12.00');
+ });
+ });
+
+ describe('amountToCurrencyInteger', () => {
+ test('empty string (None) uses 2dp', () => {
+ expect(amountToCurrencyInteger(12.34, '')).toBe(1234);
+ });
+
+ test('USD uses 2dp', () => {
+ expect(amountToCurrencyInteger(12.34, 'USD')).toBe(1234);
+ });
+
+ test('JPY uses 0dp (multiply by 1)', () => {
+ expect(amountToCurrencyInteger(1234, 'JPY')).toBe(1234);
+ });
+
+ test('IRR uses 0dp (multiply by 1)', () => {
+ expect(amountToCurrencyInteger(1234, 'IRR')).toBe(1234);
+ });
+
+ it.each<[number, string]>([
+ [12.34, 'USD'],
+ [1234, 'JPY'],
+ [5678, 'IRR'],
+ ])(
+ 'round-trip amountToCurrencyInteger/integerToCurrencyWithDecimal: %s %s',
+ (amount, currency) => {
+ setNumberFormat({ format: 'comma-dot', hideFraction: false });
+ const encoded = amountToCurrencyInteger(amount, currency);
+ const decoded = parseFloat(
+ integerToCurrencyWithDecimal(encoded, currency).replace(/,/g, ''),
+ );
+ expect(decoded).toBeCloseTo(amount, 8);
+ },
+ );
+ });
});
diff --git a/packages/loot-core/src/shared/util.ts b/packages/loot-core/src/shared/util.ts
index 3d9b32cad0..ccf7658f67 100644
--- a/packages/loot-core/src/shared/util.ts
+++ b/packages/loot-core/src/shared/util.ts
@@ -2,6 +2,8 @@
import { formatDistanceToNow } from 'date-fns';
import type { Locale } from 'date-fns';
+import { getDecimalPlaces } from './currencies';
+
export function last<T>(arr: Array<T>) {
return arr[arr.length - 1];
}
@@ -453,7 +455,19 @@ export function integerToCurrency(
return formatter.format(amount);
}
-export function integerToCurrencyWithDecimal(integerAmount: IntegerAmount) {
+export function integerToCurrencyWithDecimal(
+ integerAmount: IntegerAmount,
+ currencyCode?: string,
+) {
+ if (currencyCode !== undefined) {
+ const dp = getDecimalPlaces(currencyCode);
+ return integerToCurrency(
+ integerAmount,
+ getNumberFormat({ ...numberFormatConfig, decimalPlaces: dp }).formatter,
+ dp,
+ );
+ }
+
// If decimal digits exist, keep them. Otherwise format them as usual.
if (integerAmount % 100 !== 0) {
return integerToCurrency(
@@ -468,6 +482,10 @@ export function integerToCurrencyWithDecimal(integerAmount: IntegerAmount) {
return integerToCurrency(integerAmount);
}
+export function amountToCurrencyInteger(amount: number, code: string): number {
+ return amountToInteger(amount, getDecimalPlaces(code));
+}
+
export function amountToCurrency(amount: Amount): CurrencyAmount {
return getNumberFormat().formatter.format(amount);
}
diff --git a/upcoming-release-notes/8064.md b/upcoming-release-notes/8064.md
new file mode 100644
index 0000000000..a6feba4bdf
--- /dev/null
+++ b/upcoming-release-notes/8064.md
@@ -0,0 +1,6 @@
+---
+category: Maintenance
+authors: [StephenBrown2]
+---
+
+Adds internal currency-aware helpers to `currencies.ts` and `util.ts`.