# General Translation Platform: formatNum URL: https://generaltranslation.com/ja/docs/platform/core/reference/gt-class-methods/formatting/format-num.mdx --- title: formatNum description: ロケールに応じて、数値、通貨、パーセンテージ、その他の数値を整形します。formatNum の API リファレンス。 --- [GT](/docs/platform/core/reference/gt-class/constructor) インスタンスで、ロケール固有の規則に従って数値を整形します。General Translation は、組み込みの `Intl.NumberFormat` API を使用して、小数点区切り文字、桁区切り文字、数字体系を対象ロケールに合わせて自動的に処理します。 ## 概要 [#overview] [`GT`](/docs/platform/core/reference/gt-class/constructor) インスタンスに対して、整形する数値と任意のオプションオブジェクトを指定して `formatNum` を呼び出します。戻り値は、整形後の数値を表す文字列です。 ```typescript const gt = new GT({ targetLocale: 'de' }); const formatted = gt.formatNum(1234.56, { style: 'decimal', minimumFractionDigits: 2, }); // "1.234,56" (ドイツ語の数値フォーマット) ``` シグネチャ: ```typescript formatNum( number: number, options?: { locales?: string | string[] } & Intl.NumberFormatOptions ): string ``` *注: `formatNum` は `Intl.NumberFormat` を使ってローカルで実行されるため、APIキーは不要です。デフォルトではインスタンスの対象ロケールでフォーマットされ、ソースロケール、続いてライブラリのデフォルト (`en`) の順にフォールバックします。上書きするには `locales` を渡してください。`GT` インスタンスを使わずにフォーマットする場合は、スタンドアロンの [`formatNum`](/docs/platform/core/reference/utility-functions/formatting/format-num) を参照してください。* ## 動作の仕組み [#how-it-works] * **ロケールの解決。** デフォルトでは、このメソッドはインスタンスの対象ロケールで書式設定を行い、`locales` 設定配列ではなく、ソースロケール、次にライブラリのデフォルト (`en`) へとフォールバックします。1 回の呼び出しに対してのみ上書きするには、options で `locales` を渡してください。 * **Intl ベース。** 書式設定はブラウザネイティブの [`Intl.NumberFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat) に委譲されるため、標準の `Intl.NumberFormatOptions` をすべてサポートし、ロケールの規則も自動的に適用されます。 * **スタイルの要件。** Currency の書式設定には、`style: 'currency'` と有効な `currency` コードの両方が必要です。unit の書式設定には、`style: 'unit'` と有効な `unit` 識別子の両方が必要です。 ## パラメーター [#parameters] | パラメーター | 説明 | 型 | 任意 | デフォルト | | --------------------- | ------------------------------------------------------------------ | -------- | --- | ----- | | [`number`](#number) | 書式設定する数値。 | `number` | いいえ | — | | [`options`](#options) | `locales` のオーバーライドを追加した `Intl.NumberFormatOptions` を拡張した、書式設定用の構成。 | `object` | はい | — | ### `number` [#number] **Type** `number` · **必須** フォーマットする数値です。 ### `options` [#options] **Type** `{ locales?: string | string[] } & Intl.NumberFormatOptions` · **任意** 書式設定用のオプションです。追加の `locales` フィールドによって `Intl.NumberFormatOptions` を拡張します: | Name | Description | Type | Optional | Default | | -------------------------- | --------------------------------------- | -------------------------------------------------------------------------------------------------------------------- | -------- | -------------- | | `locales` | 書式設定に使用するロケールを上書きします。 | `string \| string[]` | はい | インスタンスの対象ロケール | | `style` | 数値の書式設定スタイル。 | `'decimal' \| 'currency' \| 'percent' \| 'unit'` | はい | `'decimal'` | | `currency` | 通貨コード (`style` が `'currency'` の場合は必須) 。 | `string` | はい | — | | `currencyDisplay` | 通貨の表示方法。 | `'symbol' \| 'narrowSymbol' \| 'code' \| 'name'` | はい | `'symbol'` | | `currencySign` | 通貨の符号形式。 | `'standard' \| 'accounting'` | はい | `'standard'` | | `unit` | 単位識別子 (`style` が `'unit'` の場合は必須) 。 | `string` | はい | — | | `unitDisplay` | 単位の表示方法。 | `'short' \| 'narrow' \| 'long'` | はい | `'short'` | | `minimumIntegerDigits` | 整数部の最小桁数 (1–21) 。 | `number` | はい | `1` | | `minimumFractionDigits` | 小数部の最小桁数 (0–20) 。 | `number` | はい | — | | `maximumFractionDigits` | 小数部の最大桁数 (0–20) 。 | `number` | はい | — | | `minimumSignificantDigits` | 有効桁数の最小値 (1–21) 。 | `number` | はい | — | | `maximumSignificantDigits` | 有効桁数の最大値 (1–21) 。 | `number` | はい | — | | `useGrouping` | 桁区切り記号を使用するかどうか。 | `boolean \| 'always' \| 'auto' \| 'min2'` | はい | `'auto'` | | `notation` | 数値表記の形式。 | `'standard' \| 'scientific' \| 'engineering' \| 'compact'` | はい | `'standard'` | | `compactDisplay` | コンパクト表記の表示スタイル。 | `'short' \| 'long'` | はい | `'short'` | | `signDisplay` | 符号を表示するタイミング。 | `'auto' \| 'never' \| 'always' \| 'exceptZero'` | はい | `'auto'` | | `roundingMode` | 丸めモード。 | `'ceil' \| 'floor' \| 'expand' \| 'trunc' \| 'halfCeil' \| 'halfFloor' \| 'halfExpand' \| 'halfTrunc' \| 'halfEven'` | はい | `'halfExpand'` | | `roundingIncrement` | 丸め単位。 | `1 \| 2 \| 5 \| 10 \| 20 \| 25 \| 50 \| 100` | はい | `1` | | `trailingZeroDisplay` | 末尾のゼロを表示するかどうか。 | `'auto' \| 'stripIfInteger'` | はい | `'auto'` | ## 戻り値 [#returns] **型** `string` 対象のロケールの規則に従って書式設定された数値です。 ## 例 [#examples] ```typescript import { GT } from 'generaltranslation'; const gt = new GT({ targetLocale: 'en-US' }); // 基本的な小数のフォーマット console.log(gt.formatNum(1234.567)); // 出力: "1,234.567" // ドイツ語ロケールのフォーマット console.log(gt.formatNum(1234.567, { locales: 'de-DE' })); // 出力: "1.234,567" // フランス語ロケールのフォーマット console.log(gt.formatNum(1234.567, { locales: 'fr-FR' })); // 出力: "1 234,567" ``` ```typescript // Currencyフォーマット // 米ドルのフォーマット console.log(gt.formatNum(1234.56, { style: 'currency', currency: 'USD', })); // 出力: "$1,234.56" // ドイツ語ロケールでのユーロのフォーマット console.log(gt.formatNum(1234.56, { style: 'currency', currency: 'EUR', locales: 'de-DE', })); // 出力: "1.234,56 €" // Currency表示オプション console.log(gt.formatNum(1234.56, { style: 'currency', currency: 'USD', currencyDisplay: 'code', })); // 出力: "USD 1,234.56" // 会計フォーマット(負の値を括弧で表示) console.log(gt.formatNum(-1234.56, { style: 'currency', currency: 'USD', currencySign: 'accounting', })); // 出力: "($1,234.56)" ``` ```typescript // パーセンテージと指数表記 // 基本的なパーセンテージ console.log(gt.formatNum(0.1234, { style: 'percent' })); // 出力: "12%" // 小数点以下の桁数を指定したパーセンテージ console.log(gt.formatNum(0.1234, { style: 'percent', minimumFractionDigits: 1, maximumFractionDigits: 2, })); // 出力: "12.34%" // コンパクト表記 console.log(gt.formatNum(1234567, { notation: 'compact' })); // 出力: "1.2M" // 指数表記 console.log(gt.formatNum(1234567, { notation: 'scientific' })); // 出力: "1.235E6" ``` ## メモ [#notes] * 数値の書式は、ロケール固有の規則に従って自動的に適用されます。 * このメソッドは、パフォーマンスと正確性を確保するために、ブラウザー標準の `Intl.NumberFormat` を使用します。 * Currencyの書式設定には、`style: 'currency'` と有効な `currency` コードの両方が必要です。 * 単位の書式設定には、`style: 'unit'` と有効な `unit` 識別子の両方が必要です。