Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/components/AddExpense/SplitTypeSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
},
{
Expand Down
16 changes: 16 additions & 0 deletions src/utils/numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down