# generaltranslation: General Translation Core SDK: formatNum URL: https://generaltranslation.com/ja/docs/core/functions/formatting/format-num.mdx --- title: formatNum description: ロケールの規則に従って数値を整形する、スタンドアロンで動作する関数 --- ## 概要 スタンドアロンの `formatNum` 関数は、GT インスタンスを必要とせず、ロケール固有の規則に従って数値を整形します。 GT クラスメソッドと同じ機能を提供しますが、単独でも使用できます。 ```typescript import { formatNum } from 'generaltranslation'; const formatted = formatNum(1234.56, { locales: 'de-DE', style: 'currency', currency: 'EUR' }); // 返り値: "1.234,56 €" ``` ## リファレンス ### パラメータ | 名前 | 型 | 説明 | | --------- | ------------------------------------------------------- | -------------------------- | | `number` | `number` | 書式設定する数値 | | `options` | `NumberFormatOptions & { locales: string \| string[] }` | 必須の `locales` を含む書式設定オプション | ### NumberFormatOptions | Name | Type | Description | | ------------------------ | ---------------------------------------------------------- | ------------------------------------ | | `locales` | `string \| string[]` | **必須** - 書式設定に使用するロケール | | `style?` | `'decimal' \| 'currency' \| 'percent' \| 'unit'` | 数値の書式 | | `currency?` | `string` | 通貨コード (style が `'currency'` の場合は必須) | | `minimumIntegerDigits?` | `number` | 整数部の最小桁数 (1〜21) | | `minimumFractionDigits?` | `number` | 小数部の最小桁数 (0〜20) | | `maximumFractionDigits?` | `number` | 小数部の最大桁数 (0〜20) | | `useGrouping?` | `boolean \| 'always' \| 'auto' \| 'min2'` | 桁区切り記号を使用するかどうか | | `notation?` | `'standard' \| 'scientific' \| 'engineering' \| 'compact'` | 数値表記の形式 | ### 戻り値 `string` - ロケールの慣習に従ってフォーマットされた数値を表す文字列です。 *** ## 例 ### 基本的な使い方 ```typescript copy import { formatNum } from 'generaltranslation'; // 基本的な数値フォーマット console.log(formatNum(1234.567, { locales: 'en-US' })); // 出力: "1,234.567" // ドイツ語フォーマット console.log(formatNum(1234.567, { locales: 'de-DE' })); // 出力: "1.234,567" ``` ### 通貨のフォーマット ```typescript copy // 米ドル console.log(formatNum(1234.56, { locales: 'en-US', style: 'currency', currency: 'USD' })); // Output: "$1,234.56" // ドイツロケールのユーロ console.log(formatNum(1234.56, { locales: 'de-DE', style: 'currency', currency: 'EUR' })); // Output: "1.234,56 €" // 日本円 console.log(formatNum(1234.56, { locales: 'ja-JP', style: 'currency', currency: 'JPY' })); // Output: "¥1,235" ``` *** ## 注意事項 * GT クラスメソッドとは異なり、`locales` パラメータは必須です * GT クラスメソッドと同じ `Intl.NumberFormat` を内部で使用します * パフォーマンス向上のため、同じロケールとオプションの組み合わせでは結果が内部的にキャッシュされます * プライマリロケールがサポートされていない場合は、フォールバックロケールが順に処理されます * `Intl.NumberFormat` の標準オプションをすべてサポートしています ## 次のステップ * より多くのオプションについては、[`Intl.NumberFormat` のドキュメント](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat)を参照してください * スタンドアロンでの日付フォーマットについては、[`formatDateTime`](/docs/core/functions/formatting/format-date-time)を参照してください * スタンドアロンでのメッセージフォーマットについては、[`formatMessage`](/docs/core/functions/formatting/format-message)を参照してください * インスタンスベースの使用方法については、GT クラスの [`formatNum`](/docs/core/class/methods/formatting/format-num)を参照してください