--- title: Format description: "`Format`は、日付・数値・バイトなどを特定のロケールに合わせてフォーマットするために使用されます。" links: - source: https://github.com/yamada-ui/yamada-ui/tree/main/packages/react/src/components/format - storybook: https://yamada-ui.github.io/yamada-ui?path=/story/components-format --- ```tsx ``` ## 使い方 ```tsx import { Format } from "@yamada-ui/react" ``` ```tsx import { Format } from "@/components/ui" ``` ```tsx import { Format } from "@workspaces/ui" ``` ```tsx ``` ### 日付 :::note `Format.DateTime`は、内部で[Intl.DateTimeFormat](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat)を使用しています。 ::: #### ロケールを変更する ロケールを変更する場合は、[locale](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#locales)に値を設定します。 ```tsx 英語 日本語 ドイツ語 フランス語 中国語 ``` #### 年に変換する 年に変換する場合は、[year](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#year)に値を設定します。 ```tsx ``` #### 月に変換する 月に変換する場合は、[month](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#month)に値を設定します。 ```tsx ``` #### 日に変換する 日に変換する場合は、[day](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#day)に値を設定します。 ```tsx ``` #### 曜日に変換する 曜日に変換する場合は、[weekday](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#weekday)に値を設定します。 ```tsx ``` ### 数値 :::note `Format.Number`は、内部で[Intl.NumberFormat](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat)を使用しています。 ::: #### ロケールを変更する ロケールを変更する場合は、[locale](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#locales)に値を設定します。 ```tsx 英語 日本語 ドイツ語 フランス語 中国語 ``` #### 通貨に変換する 通貨に変換する場合は、[style](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#style)に`"currency"`を設定します。 ```tsx 米ドル ユーロ ``` #### 単位に変換する 単位に変換する場合は、[style](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#style)に`"unit"`を設定します。 ```tsx ``` #### パーセントに変換する パーセントに変換する場合は、[style](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#style)に`"percent"`を設定します。 ```tsx ``` #### 表記に変換する 表記に変換する場合は、[notation](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#notation)に値を設定します。 ```tsx ``` #### 小数点以下の桁数を制御する 小数点以下の桁数を制御する場合は、[minimumFractionDigits](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#minimumfractiondigits)と[maximumFractionDigits](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#maximumfractiondigits)を使用します。 ```tsx ``` #### グループ化を無効にする グループ化を無効にする場合は、[useGrouping](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#usegrouping)を`false`に設定します。 ```tsx ``` #### 符号の表示を変更する 符号の表示を変更する場合は、[signDisplay](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#signdisplay)に値を設定します。 ```tsx ``` ### バイト :::note `Format.Byte`は、内部で[Intl.NumberFormat](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat)を使用しています。 ::: #### 自動単位選択 `Format.Byte`は、バイト値の大きさに基づいて最適な単位(`byte`、`kB`、`MB`、`GB`、`TB`)を自動的に選択します。 ```tsx ``` #### ロケールを変更する ロケールを変更する場合は、[locale](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#locales)に値を設定します。 ```tsx en-US ja-JP de-DE fr-FR zh-CN ``` #### 単位を変換する 単位を変換する場合は、`unit`に`"byte"`または`"bit"`を設定します。デフォルトでは、`"byte"`が設定されています。 ```tsx バイト ビット ``` #### 単位の表示を変更する 単位の表示を変更する場合は、[unitDisplay](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#unitdisplay)に値を設定します。 ```tsx Short Narrow Long ``` ### アプリケーション全体のロケールを設定する アプリケーション全体のロケールの設定をする場合は、`UIProvider`の`locale`に値を設定します。 ```tsx import { UIProvider } from "@yamada-ui/react" const App = () => { return ( ) } ``` ## Props ### Format.Byte | Prop | Default | Type | Description | | ------------- | --------- | ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------ | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | | `value` | - | `number` | The byte size to format | | `locale` | `"en-US"` | `AnyString \| Locale` | The locale string to use for formatting. | | `unit` | - | `"bit" \| "byte"` | The unit granularity to display | | `unitDisplay` | - | `"long" \| "narrow" \| "short"` | The unit display | ### Format.DateTime | Prop | Default | Type | Description | | ------------- | --------- | ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------ | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | | `value` | - | `Date` | The date time to format | | `locale` | `"en-US"` | `AnyString \| Locale` | The locale string to use for formatting. | ### Format.Number | Prop | Default | Type | Description | | ------------- | --------- | ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------ | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | | `value` | - | `number` | The numeric value to be formatted. | | `locale` | `"en-US"` | `AnyString \| Locale` | The locale string to use for formatting. |