--- title: useFormatNumber description: "`useFormatNumber` is a custom hook for formatting numbers." links: - source: https://github.com/yamada-ui/yamada-ui/tree/main/packages/react/src/hooks/use-format-number - storybook: https://yamada-ui.github.io/yamada-ui?path=/story/hooks-useformatnumber--basic --- ```tsx const formattedValue = useFormatNumber(1234567.89) return {formattedValue} ``` ## Usage ```tsx import { useFormatNumber } from "@yamada-ui/react" ``` ```tsx import { useFormatNumber } from "@/components/ui" ``` ```tsx import { useFormatNumber } from "@workspaces/ui" ``` ```tsx const formattedValue = useFormatNumber(1234567.89) ``` It formats numbers according to the specified locale and options. The hook returns the formatted value directly. :::note `useFormatNumber` internally uses [Intl.NumberFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat). ::: ### Changing the Locale To change the locale, set a value for [locale](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#locales). ```tsx const enValue = useFormatNumber(1234567.89, { locale: "en-US" }) const jaValue = useFormatNumber(1234567.89, { locale: "ja-JP" }) const deValue = useFormatNumber(1234567.89, { locale: "de-DE" }) const frValue = useFormatNumber(1234567.89, { locale: "fr-FR" }) const zhValue = useFormatNumber(1234567.89, { locale: "zh-CN" }) return ( en-US {enValue} ja-JP {jaValue} de-DE {deValue} fr-FR {frValue} zh-CN {zhValue} ) ``` ### Set the Locale for the Entire Application If you want to set the locale for the entire application, set the `locale` for the `UIProvider`. ```tsx import { UIProvider } from "@yamada-ui/react" const App = () => { return ( ) } ``` ### Converting to Currency To convert to currency, set `"currency"` for [style](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#style). ```tsx const usdValue = useFormatNumber(1234567.89, { style: "currency", currency: "USD", locale: "en-US", }) const eurValue = useFormatNumber(1234567.89, { style: "currency", currency: "EUR", locale: "de-DE", }) const jpyValue = useFormatNumber(1234567.89, { style: "currency", currency: "JPY", locale: "ja-JP", }) return ( USD {usdValue} EUR {eurValue} JPY {jpyValue} ) ``` ### Converting to Units To convert to units, set `"unit"` for [style](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#style). ```tsx const kilogramValue = useFormatNumber(100, { style: "unit", unit: "kilogram", }) const celsiusValue = useFormatNumber(100, { style: "unit", unit: "celsius", unitDisplay: "long", }) const speedValue = useFormatNumber(100, { style: "unit", unit: "kilometer-per-hour", unitDisplay: "narrow", }) return ( {kilogramValue} {celsiusValue} {speedValue} ) ``` ### Converting to Percent To convert to percent, set `"percent"` for [style](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#style). ```tsx const percent1Value = useFormatNumber(0.45, { style: "percent" }) const percent2Value = useFormatNumber(0.45, { style: "percent", minimumFractionDigits: 2, }) return ( {percent1Value} {percent2Value} ) ``` ### Converting Notation To convert notation, set a value for [notation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#notation). ```tsx const standardValue = useFormatNumber(1234567.89, { notation: "standard" }) const scientificValue = useFormatNumber(1234567.89, { notation: "scientific" }) const engineeringValue = useFormatNumber(1234567.89, { notation: "engineering", }) const compactValue = useFormatNumber(1234567.89, { notation: "compact" }) return ( {standardValue} {scientificValue} {engineeringValue} {compactValue} ) ``` ### Controlling Decimal Places To control the number of decimal places, use [minimumFractionDigits](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#minimumfractiondigits) and [maximumFractionDigits](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#maximumfractiondigits). ```tsx const fixed2Value = useFormatNumber(1234.5, { minimumFractionDigits: 2, maximumFractionDigits: 2, }) const range03Value = useFormatNumber(1234.567, { minimumFractionDigits: 0, maximumFractionDigits: 3, }) const fixed4Value = useFormatNumber(1234, { minimumFractionDigits: 4, maximumFractionDigits: 4, }) return ( {fixed2Value} {range03Value} {fixed4Value} ) ``` ### Disabling Grouping To disable grouping, set `false` for [useGrouping](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#usegrouping). ```tsx const noGroupingValue = useFormatNumber(1234567.89, { useGrouping: false }) return {noGroupingValue} ``` ### Changing the Sign Display To change the sign display, set a value for [signDisplay](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#signdisplay). ```tsx const alwaysValue = useFormatNumber(1234.5, { signDisplay: "always" }) const exceptZeroValue = useFormatNumber(-1234.5, { signDisplay: "exceptZero" }) return ( {alwaysValue} {exceptZeroValue} ) ```