Skip to main content

useMoney

The useMoney hook takes a MoneyV2 object and returns a default-formatted string of the amount with the correct currency indicator, along with some of the parts provided by Intl.NumberFormat.

Example code

import {useMoney} from '@shopify/hydrogen';

export function MyComponent() {
const [value, parts] = useMoney(variant.pricev2);

return (
<div>
<strong>{parts.currencyCode}</strong>
<span>{parts.currencySymbol}</span>
<span>{parts.amount}</span>
</div>
);
}

Return value

This hook returns an object with the following keys:

KeyDescription
localizedStringA string returned by new Intl.NumberFormat for the amount and currency code, using the locale value from the ShopifyProvider component.
currencyCodeThe currency code from the MoneyV2 object.
currencyNameThe name for the currency code, returned by Intl.NumberFormat.
currencySymbolThe currency symbol returned by Intl.NumberFormat.
currencyNarrowSymbolThe currency narrow symbol returned by Intl.NumberFormat.
amountThe localized amount, without any currency symbols or non-number types from the Intl.NumberFormat.formatToParts parts.
partsAll parts returned by Intl.NumberFormat.formatToParts.
originalThe original MoneyV2 object passed as an argument.
withoutTrailingZerosA string with trailing zeros removed from the fractional part, if any exist. If there are no trailing zeros, then the fractional part remains. For example, $640.00 turns into $640. $640.42 turns into $640.42.
withoutTrailingZerosAndCurrencyA string without currency and without trailing zeros removed from the fractional part, if any exist. If there are no trailing zeros, then the fractional part remains. For example, $640.00 turns into 640. $640.42 turns into 640.42.