--- title: useFormatByte description: "`useFormatByte` is a custom hook for formatting bytes." links: - source: https://github.com/yamada-ui/yamada-ui/tree/main/packages/react/src/hooks/use-format-byte - storybook: https://yamada-ui.github.io/yamada-ui?path=/story/hooks-useformatbyte--basic --- ```tsx const kilobyte = useFormatByte(1024) const megabyte = useFormatByte(1024 * 1024) const gigabyte = useFormatByte(1024 * 1024 * 1024) const terabyte = useFormatByte(1024 * 1024 * 1024 * 1024) return ( <> {kilobyte} {megabyte} {gigabyte} {terabyte} ) ``` ## Usage ```tsx import { useFormatByte } from "@yamada-ui/react" ``` ```tsx import { useFormatByte } from "@/components/ui" ``` ```tsx import { useFormatByte } from "@workspaces/ui" ``` ```tsx const kilobyte = useFormatByte(1024) ``` :::note `FormatByte` automatically selects the most appropriate unit (`byte`, `kB`, `MB`, `GB`, `TB`) based on the byte value size. ::: :::note `useFormatByte` 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 enByte = useFormatByte(1024, { locale: "en-US" }) const jaByte = useFormatByte(1024, { locale: "ja-JP" }) const deByte = useFormatByte(1024, { locale: "de-DE" }) return ( en-US {enByte} ja-JP {jaByte} de-DE {deByte} ) ``` ### 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 ( ) } ``` ### Unit Format To convert units, set `unit` to either `"byte"` or `"bit"`. The default is `"byte"`. ```tsx const bytes = useFormatByte(1024, { unit: "byte" }) const bits = useFormatByte(1024, { unit: "bit" }) return ( Bytes {bytes} Bits {bits} ) ``` ### Unit Display To change the unit display, set a value for [unitDisplay](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#unitdisplay). ```tsx const short = useFormatByte(1024, { unitDisplay: "short" }) const narrow = useFormatByte(1024, { unitDisplay: "narrow" }) const long = useFormatByte(1024, { unitDisplay: "long" }) return ( Short {short} Narrow {narrow} Long {long} ) ```