GT ClassMethodsFormatting
formatNum
locale の規約に基づいて数値をフォーマットする formatNum メソッドのAPIリファレンス
概要
formatNum メソッドは Internationalization API を使用し、locale ごとの慣習に従って数値を整形します。
指定した locale に基づいて、小数点記号、桁区切り、番号体系を自動的に処理します。
const gt = new GT({ targetLocale: 'de' });
const formatted = gt.formatNum(1234.56, {
  style: 'decimal',
  minimumFractionDigits: 2
});
// 戻り値: "1.234,56" (ドイツ語の数値フォーマット)リファレンス
パラメータ
| Name | Type | Description | 
|---|---|---|
| number | number | 形式化する数値 | 
| options? | NumberFormatOptions | 省略可能な書式設定 | 
NumberFormatOptions
Intl.NumberFormatOptions に、追加のロケール指定を加えて拡張します:
| Name | Type | Description | 
|---|---|---|
| locales? | string | string[] | フォーマット時に使用する対応ロケールを上書き(既定はインスタンスの対応ロケール) | 
| style? | 'decimal' | 'currency' | 'percent' | 'unit' | 数値の書式スタイル | 
| currency? | string | 通貨コード(style が 'currency' の場合は必須) | 
| currencyDisplay? | 'symbol' | 'narrowSymbol' | 'code' | 'name' | 通貨の表示方法 | 
| currencySign? | 'standard' | 'accounting' | 使用する通貨記号の種別 | 
| unit? | string | 単位識別子(style が 'unit' の場合は必須) | 
| unitDisplay? | 'short' | 'narrow' | 'long' | 単位の表示方法 | 
| minimumIntegerDigits? | number | 整数桁の最小値(1〜21) | 
| minimumFractionDigits? | number | 小数桁の最小値(0〜20) | 
| maximumFractionDigits? | number | 小数桁の最大値(0〜20) | 
| minimumSignificantDigits? | number | 有効桁数の最小値(1〜21) | 
| maximumSignificantDigits? | number | 有効桁数の最大値(1〜21) | 
| useGrouping? | boolean | 'always' | 'auto' | 'min2' | 桁区切りを使用するか | 
| notation? | 'standard' | 'scientific' | 'engineering' | 'compact' | 数値の表記形式 | 
| compactDisplay? | 'short' | 'long' | コンパクト表記の表示スタイル | 
| signDisplay? | 'auto' | 'never' | 'always' | 'exceptZero' | 符号を表示する条件 | 
| roundingMode? | 'ceil' | 'floor' | 'expand' | 'trunc' | 'halfCeil' | 'halfFloor' | 'halfExpand' | 'halfTrunc' | 'halfEven' | 丸めモード | 
| roundingIncrement? | 1 | 2 | 5 | 10 | 20 | 25 | 50 | 100 | 丸めのインクリメント | 
| trailingZeroDisplay? | 'auto' | 'stripIfInteger' | 末尾のゼロの表示有無 | 
戻り値
string - locale の規約に従って書式設定された数値。
例
基本的な数値のフォーマット
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"通貨のフォーマット
// 米ドル形式
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 €"
// 通貨表示オプション
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)"パーセンテージと科学的表記
// 基本的なパーセンテージ
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"注意事項
- 数値の書式は、locale に応じた規則が自動的に適用されます
- このメソッドは、最適なパフォーマンスと精度のためにブラウザ標準の Intl.NumberFormat を使用します
- 通貨の書式設定には、style: 'currency' と有効な currency コードの両方が必要です
- 単位の書式設定には、style: 'unit' と有効な unit 識別子の両方が必要です
次のステップ
- さらに多くの options については、Intl.NumberFormatのドキュメントを参照してください
- 通貨専用のフォーマットは format-currencyを参照
- 数値の補間を含むメッセージのフォーマットは format-messageを参照
- GT インスタンスなしで使う場合は、スタンドアロンの format-numを参照
- ロケール固有のフォーマット情報は get-locale-propertiesを参照
このガイドはどうでしたか?

