Packages
react
hooks
useMoney

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.

Parameters

money

  • Type: MoneyV2
  • Required: true

Example Usage

import React from "react";
import { useMoney } from "./useMoney";
 
const MyComponent = ({ money }) => {
  const moneyDetails = useMoney(money);
 
  return (
    <div>
      <p>Amount: {moneyDetails.amount}</p>
      <p>Currency Code: {moneyDetails.currencyCode}</p>
      <p>Currency Symbol: {moneyDetails.currencySymbol}</p>
    </div>
  );
};
 
export default MyComponent;

Returns

UseMoneyValue

PropertyTypeDescription
currencyCodeCurrencyCodeThe currency code from the MoneyV2 object.
amountstringThe localized amount, without any currency symbols or non-number types from the Intl.NumberFormat.formatToParts parts.
partsNumberFormatPart[]All parts returned by Intl.NumberFormat.formatToParts.
localizedStringstringA string returned by new Intl.NumberFormat for the amount and currency code, using the locale value in the LocalizationProvider component.
originalMoneyV2The MoneyV2 object provided as an argument to the hook.
withoutTrailingZerosstringA 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 remains $640.42.
withoutTrailingZerosAndCurrencystringA 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.
currencyNamestringThe name for the currency code, returned by Intl.NumberFormat.
currencySymbolstringThe currency symbol returned by Intl.NumberFormat.
currencyNarrowSymbolstringThe currency narrow symbol returned by Intl.NumberFormat.