Skip to content

A local-first personal finance app

fix: reapply thousand separators before passing input to appendDecimals (#5220)

An Hoang94a76a008dcc
diff --git a/packages/desktop-client/src/components/mobile/transactions/FocusableAmountInput.tsx b/packages/desktop-client/src/components/mobile/transactions/FocusableAmountInput.tsx
index 81a283811f..85f043314e 100644
--- a/packages/desktop-client/src/components/mobile/transactions/FocusableAmountInput.tsx
+++ b/packages/desktop-client/src/components/mobile/transactions/FocusableAmountInput.tsx
@@ -19,6 +19,7 @@ import {
amountToCurrency,
appendDecimals,
currencyToAmount,
+ reapplyThousandSeparators,
} from 'loot-core/shared/util';
import { makeAmountFullStyle } from '@desktop-client/components/budget/util';
@@ -113,6 +114,7 @@ const AmountInput = memo(function AmountInput({
};
const onChangeText = (text: string) => {
+ text = reapplyThousandSeparators(text);
text = appendDecimals(text, String(hideFraction) === 'true');
setEditing(true);
setText(text);
diff --git a/packages/desktop-client/src/components/util/AmountInput.tsx b/packages/desktop-client/src/components/util/AmountInput.tsx
index 0df7a5283c..b3711e9731 100644
--- a/packages/desktop-client/src/components/util/AmountInput.tsx
+++ b/packages/desktop-client/src/components/util/AmountInput.tsx
@@ -18,7 +18,11 @@ import { View } from '@actual-app/components/view';
import { css, cx } from '@emotion/css';
import { evalArithmetic } from 'loot-core/shared/arithmetic';
-import { amountToInteger, appendDecimals } from 'loot-core/shared/util';
+import {
+ amountToInteger,
+ appendDecimals,
+ reapplyThousandSeparators,
+} from 'loot-core/shared/util';
import { useFormat } from '@desktop-client/hooks/useFormat';
import { useMergedRefs } from '@desktop-client/hooks/useMergedRefs';
@@ -96,6 +100,7 @@ export function AmountInput({
}
function onInputTextChange(val) {
+ val = reapplyThousandSeparators(val);
val = autoDecimals
? appendDecimals(val, String(hideFraction) === 'true')
: val;
diff --git a/packages/loot-core/src/shared/util.ts b/packages/loot-core/src/shared/util.ts
index cb6a32ff9c..f384300ff1 100644
--- a/packages/loot-core/src/shared/util.ts
+++ b/packages/loot-core/src/shared/util.ts
@@ -214,6 +214,29 @@ export function titleFirst(str: string | null | undefined) {
return str[0].toUpperCase() + str.slice(1);
}
+export function reapplyThousandSeparators(amountText: string) {
+ if (!amountText || typeof amountText !== 'string') {
+ return amountText;
+ }
+
+ const { decimalSeparator, thousandsSeparator } = getNumberFormat();
+ const [integerPartRaw, decimalPart = ''] = amountText.split(decimalSeparator);
+
+ const numericValue = Number(
+ integerPartRaw.replaceAll(thousandsSeparator, ''),
+ );
+ if (isNaN(numericValue)) {
+ return amountText; // Return original if parsing fails
+ }
+
+ const integerPart = numericValue
+ .toLocaleString('en-US')
+ .replaceAll(',', thousandsSeparator);
+ return decimalPart
+ ? integerPart + decimalSeparator + decimalPart
+ : integerPart;
+}
+
export function appendDecimals(
amountText: string,
hideDecimals = false,
diff --git a/upcoming-release-notes/5220.md b/upcoming-release-notes/5220.md
new file mode 100644
index 0000000000..866d346436
--- /dev/null
+++ b/upcoming-release-notes/5220.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [intagaming]
+---
+
+Reapply thousand separators before passing input to appendDecimals