Skip to content

Commit

Permalink
addressing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrans committed Apr 16, 2024
1 parent 8d12283 commit 9be2894
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
4 changes: 2 additions & 2 deletions ui/hooks/useCurrencyDisplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const MIN_AMOUNT_DISPLAY = `<${MIN_AMOUNT}`;

// The default precision for displaying currency values.
// It set to the number of decimal places in the minimum amount.
export const MIN_AMOUNT_DECIMALS = new BigNumber(MIN_AMOUNT).decimalPlaces();
export const DEFAULT_PRECISION = new BigNumber(MIN_AMOUNT).decimalPlaces();

/**
* Defines the shape of the options parameter for useCurrencyDisplay
Expand Down Expand Up @@ -74,7 +74,7 @@ export function useCurrencyDisplay(
) {
const ethDisplayValue = new Numeric(inputValue, 16, EtherDenomination.WEI)
.toDenomination(denomination || EtherDenomination.ETH)
.round(numberOfDecimals || MIN_AMOUNT_DECIMALS)
.round(numberOfDecimals || DEFAULT_PRECISION)
.toBase(10)
.toString();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import { hexToDecimal } from '../../../../../shared/modules/conversion.utils';
import { TokenStandard } from '../../../../../shared/constants/transaction';
import Tooltip from '../../../../components/ui/tooltip';
import { getCurrentLocale } from '../../../../ducks/locale/locale';
import { getIntlLocale } from '../../../../ducks/locale/locale';
import { AssetIdentifier } from './types';
import { formatAmount, formatAmountMaxPrecision } from './formatAmount';

Expand All @@ -30,7 +30,7 @@ export const AmountPill: React.FC<{
asset: AssetIdentifier;
amount: BigNumber;
}> = ({ asset, amount }) => {
const locale = useSelector(getCurrentLocale);
const locale = useSelector(getIntlLocale);

const backgroundColor = amount.isNegative()
? BackgroundColor.errorMuted
Expand All @@ -46,10 +46,7 @@ export const AmountPill: React.FC<{
// ERC721 amounts are always 1 and are not displayed.
if (asset.standard !== TokenStandard.ERC721) {
const formattedAmount = formatAmount(locale, amount.abs());
const fullPrecisionAmount = formatAmountMaxPrecision(
locale,
amount.abs().toNumber(),
);
const fullPrecisionAmount = formatAmountMaxPrecision(locale, amount.abs());

amountParts.push(formattedAmount);
tooltipParts.push(fullPrecisionAmount);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { BigNumber } from 'bignumber.js';
import {
MIN_AMOUNT,
MIN_AMOUNT_DECIMALS,
DEFAULT_PRECISION,
} from '../../../../hooks/useCurrencyDisplay';

// The number of significant decimals places to show for amounts less than 1.
const MAX_SIGNIFICANT_DECIMAL_PLACES = 3;

const ZERO_DISPLAY = '0';

export const formatAmountMaxPrecision = (
export function formatAmountMaxPrecision(
locale: string,
num: number | BigNumber,
): string =>
new Intl.NumberFormat(locale, {
): string {
return new Intl.NumberFormat(locale, {
minimumSignificantDigits: 1,
}).format(new BigNumber(num.toString()).toNumber());
}

/**
* Formats the a token amount with variable precision and significant
Expand Down Expand Up @@ -65,7 +66,7 @@ export function formatAmount(locale: string, amount: BigNumber): string {
return new Intl.NumberFormat(locale, {
maximumSignificantDigits: MAX_SIGNIFICANT_DECIMAL_PLACES,
} as Intl.NumberFormatOptions).format(
amount.round(MIN_AMOUNT_DECIMALS).toNumber(),
amount.round(DEFAULT_PRECISION).toNumber(),
);
}

Expand Down

0 comments on commit 9be2894

Please sign in to comment.