From 54b8ddb55bb55b8b67d3dfe0aa1adafc57b1dfd6 Mon Sep 17 00:00:00 2001 From: David Mellen Date: Mon, 6 Oct 2025 11:09:50 +0200 Subject: [PATCH] Fix split input locale decimal seperator --- src/components/AddExpense/SplitTypeSection.tsx | 4 ++-- src/utils/numbers.ts | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/components/AddExpense/SplitTypeSection.tsx b/src/components/AddExpense/SplitTypeSection.tsx index 183f6785..f566476f 100644 --- a/src/components/AddExpense/SplitTypeSection.tsx +++ b/src/components/AddExpense/SplitTypeSection.tsx @@ -14,7 +14,7 @@ import { type ChangeEvent, useCallback, useMemo } from 'react'; import { useTranslationWithUtils } from '~/hooks/useTranslationWithUtils'; import { type AddExpenseState, type Participant, useAddExpenseStore } from '~/store/addStore'; -import { removeTrailingZeros, toSafeBigInt, toUIString } from '~/utils/numbers'; +import { removeTrailingZeros, toSafeBigInt, toUINumber, toUIString } from '~/utils/numbers'; import { type TFunction, useTranslation } from 'next-i18next'; import { EntityAvatar } from '../ui/avatar'; @@ -205,7 +205,7 @@ const getSplitProps = (t: TFunction): SplitSectionProps[] => [ ); return `${t('expense_details.add_expense_details.split_type_section.types.exact.remaining')} ${currency} ${toUIString(amount - totalAmount, true)}`; }, - fmtShareText: (share) => removeTrailingZeros(toUIString(share)), + fmtShareText: (share) => removeTrailingZeros(toUINumber(share).toString()), step: null, }, { diff --git a/src/utils/numbers.ts b/src/utils/numbers.ts index da0d3e7a..2992309f 100644 --- a/src/utils/numbers.ts +++ b/src/utils/numbers.ts @@ -22,6 +22,22 @@ export function toSafeBigInt(num: number | string) { } } +export function toUINumber(num = 0n, currencyCode: CurrencyCode = 'USD') { + const { decimalDigits } = CURRENCIES[currencyCode]; + const maxDecimals = 10n ** BigInt(decimalDigits); + const decimalPart = num % 100n; + const wholePart = Number(BigMath.abs(num) / 100n); + + let result = Number(wholePart); + + if (decimalPart !== 0n) { + const frac = Number(decimalPart) / Number(maxDecimals); + result += frac; + } + + return result; +} + export function toUIString(num = 0n, signed = false, currencyCode: CurrencyCode = 'USD') { const { decimalDigits } = CURRENCIES[currencyCode]; const maxDecimals = 10n ** BigInt(decimalDigits);