Skip to content

Commit

Permalink
fix: round half up when formating amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
alfetopito committed Jan 9, 2025
1 parent 9371767 commit 9d028ac
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions libs/common-utils/src/amountFormat/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@


import {
AMOUNT_PRECISION,
FIAT_PRECISION,
INTL_NUMBER_FORMAT,
PERCENTAGE_PRECISION,
ZERO_FRACTION,
INTL_NUMBER_FORMAT,
} from '@cowprotocol/common-const'
import { Currency, CurrencyAmount, Percent, Rounding } from '@uniswap/sdk-core'

Expand Down Expand Up @@ -33,7 +31,7 @@ export function formatPercent(percent: Nullish<Percent>): string {
export function formatAmountWithPrecision(
amount: Nullish<FractionLike>,
precision: number,
numberFormat = INTL_NUMBER_FORMAT
numberFormat = INTL_NUMBER_FORMAT,
): string {
if (!amount) return ''

Expand All @@ -59,7 +57,7 @@ export function formatAmountWithPrecision(
// Apply the language formatting for the amount
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat
const formattedQuotient = numberFormat.format(
BigInt(trimTrailingZeros(adjustedQuotient.toString(), decimalsSeparator))
BigInt(trimTrailingZeros(adjustedQuotient.toString(), decimalsSeparator)),
)

// Remove trailing zeros
Expand All @@ -80,7 +78,7 @@ export function formatAmountWithPrecision(
export function formatInputAmount(
amount: Nullish<FractionLike>,
balance: Nullish<CurrencyAmount<Currency>> = null,
isIndependentField = false
isIndependentField = false,
): string {
if (!amount) return ''

Expand All @@ -92,7 +90,7 @@ export function formatInputAmount(
}

const precision = getPrecisionForAmount(amount)
const result = amount.toFixed(precision)
const result = amount.toFixed(precision, undefined, Rounding.ROUND_HALF_UP)

return trimTrailingZeros(+result === 0 ? amount.toSignificant(AMOUNT_PRECISION) : result)
}

0 comments on commit 9d028ac

Please sign in to comment.