Intl.NumberFormat【国際化数値フォーマット】 コンストラクタ・プロパティ・メソッド
new Intl.NumberFormat()【コンストラクタ】
メモ
- Intl.NumberFormat【国際化数値フォーマット】 オブジェクトを生成
- format【フォーマット関数】で文字列に変換
- 数字・通貨・桁数 等の指定が可能
- ゼロパディング:オプションに以下を指定
- useGrouping【桁区切り等の使用有無】:false
- minimumIntegerDigits【整数部の最小桁数】:桁数
- 関連:Number.toLocaleString()【文字列変換 (ロケール)】 ・new Intl.DateTimeFormat() 【国際化日時フォーマット コンストラクタ】
構文
- new Intl.NumberFormat ([ locales [, options ]])
- locales:BCP 47 の言語タグの文字列 または 配列 (省略:デフォルトのロケール)例
以下の Unicode 拡張 数値フォーマット をサポート (-u-nu- …)値 説明 ja 日本語 ja-JP 日本語 (日本) en-US 英語 (アメリカ) en-GB 英語 (イギリス) de-DE ドイツ語 (ドイツ) fr-FR フランス語 (フランス) 値 説明 例 コード arab アラビア・インド数字 ٩ ~ ٠ U+0660 ~ U+0669 arabext ペルシア数字 ۰ ~ ۹ U+06F0 ~ U+06F9 bali バリ数字 ― U+1B50 ~ U+1B59 beng ベンガル数字 ০ ~ ৯ U+09E6 ~ U+09EF deva デーヴァナーガリー数字 ० ~ ९ U+0966 ~ U+096F fullwide 全角数字 0 ~ 9 U+FF10 ~ U+FF19 gujr グジャラート数字 ૦ ~ ૯ U+0AE6 ~ U+0AEF guru グルムキー数字 ੦ ~ ੯ U+0A66 ~ U+0A6F hanidec 漢数字 〇 ~ 九 U+3007, U+4E00, U+4E8C, U+4E09, U+56DB,
U+4E94, U+516D, U+4E03, U+516B, U+4E5Dkhmr クメール数字 ០ ~ ៩ U+17E0 ~ U+17E9 knda カンナダ数字 ೦ ~ ೯ U+0CE6 ~ U+0CEF laoo ラオ数字 ໐ ~ ໙ U+0ED0 ~ U+0ED9 latn 算用数字 (アラビア数字) 0 ~ 9 U+0030 ~ U+0039 limb リンブ数字 ― U+1946 ~ U+194F mlym マラヤーラム数字 ൦ ~ ൯ U+0D66 ~ U+0D6F mong モンゴル数字 ᠐ ~ ᠙ U+1810 ~ U+1819 mymr ミャンマー数字 ၀ ~ ၉ U+1040 ~ U+1049 orya オリヤー数字 뙦 ~ 뙯 U+0B66 ~ U+0B6F tamldec タミル数字 ௦ ~ ௯ U+0BE6 ~ U+0BEF telu テルグ数字 ౦ ~ ౯ U+0C66 ~ U+0C6F thai タイ数字 ๐ ~ ๙ U+0E50 ~ U+0E59 tibt チベット数字 ༠ ~ ༩ U+0F20 ~ U+0F29 - options:オプション
オプション 値 (太字:デフォルト値) 説明 localeMatcher lookup:Lookupアルゴリズム
best fit:最適アルゴリズムロケールマッチングアルゴリズム style decimal:10進数
percent:パーセント
currency:通貨スタイル currency 通貨コード 通貨コード (ISO 4217: Wikipedia)
JPY・USD・EUR 等currencyDisplay code:ISO通貨コード
symbol:通貨シンボル
name:通貨名通貨の表示方法 minimumIntegerDigits 1 ~ 21 整数部の最小桁数 minimumFractionDigits 0 ~ 20 小数部の最小桁数 maximumFractionDigits minimumFractionDigits ~ 20 小数部の最大桁数 minimumSignificantDigits 1 ~ 21 最小有効桁数 maximumSignificantDigits minimumSignificantDigits ~ 21 最大有効桁数 useGrouping true:使用
false:未使用桁区切り等の使用有無
Intl.NumberFormat【国際化数値フォーマット】 オブジェクト
例
var num = 123456.789
var nf = new Intl.NumberFormat();
console.log(nf.format(num));
// 出力:123,456.789
nf = new Intl.NumberFormat("ja-JP", { useGrouping:false, minimumIntegerDigits:9, minimumFractionDigits:4 });
console.log(nf.format(num));
// 出力:000123456.7890
nf = new Intl.NumberFormat("ja-JP", { style:'percent' });
console.log(nf.format(0.50));
// 出力:50%
nf = new Intl.NumberFormat("ja-JP", { style:'currency', currency:'JPY' });
console.log(nf.format(num));
// 出力:¥123,457
nf = new Intl.NumberFormat("en-US", { style:'currency', currency:'USD' });
console.log(nf.format(num));
// 出力:$123,456.79
nf = new Intl.NumberFormat("de-DE", { style:'currency', currency:'EUR' });
console.log(nf.format(num));
// 出力:123.456,79 €
nf = new Intl.NumberFormat("fr-FR", { style:'currency', currency:'EUR' });
console.log(nf.format(num));
// 出力:123 456,79 €
nf = new Intl.NumberFormat("ja-JP-u-nu-hanidec", { style:'currency', currency:'JPY', minimumFractionDigits:4 });
console.log(nf.format(num));
// 出力:¥一二三,四五六.七八九〇
nf = new Intl.NumberFormat("ja-JP", { style:"currency", currency:"JPY", currencyDisplay:'name' });
console.log(nf.format(num));
// 出力:123,457円
// ゼロパディング
var maxsize = 8;
var code = 12345;
nf = new Intl.NumberFormat("ja-JP", { useGrouping:false, minimumIntegerDigits:maxsize });
console.log(nf.format(code));
// 出力:00012345
関連
- Number.toLocaleString()【文字列変換 (ロケール)】
- new Intl.DateTimeFormat () 【国際化日時フォーマット コンストラクタ】
- ECMA-402 2.0:11.2.1 Intl.NumberFormat ([ locales [ , options ]]) (英語)
- ECMA-402 2.0:11.1.1 InitializeNumberFormat (numberFormat, locales, options) (英語)
- RFC 5646:Tags for Identifying Languages (英語)
- RFC 4647:Matching of Language Tags (英語)
- Wikipedia:通貨コード (ISO 4217)
Intl.NumberFormat.prototype.format【フォーマット関数】
メモ
- フォーマット関数を返却
- フォーマット指定:new Intl.NumberFormat() 【コンストラクタ】
- Array.map ()【配列変換生成】等の利用可
フォーマット関数構文
- format ( value )
- value:数値
フォーマット文字列
例
new Intl.NumberFormat () 【コンストラクタ】の例を参照
// Array.map ()【配列変換生成】利用
var nums = [ 123, 123456, 123456789 ];
var nf = new Intl.NumberFormat();
console.log(nums.map(nf.format)); // 出力:["123", "123,456", "123,456,789"]
関連
Intl.NumberFormat.prototype.resolvedOptions()【オプション取得】
メモ
- 以下のプロパティを持ったオプション オブジェクトを取得 (デフォルト値がない未指定のプロパティは存在しない)
(new Intl.NumberFormat() 【コンストラクタ】も参照)プロパティ 説明 locale BCP47言語タグ numberingSystem Unicode 拡張 数値フォーマット style スタイル currency 通貨コード currencyDisplay 通貨の表示方法 minimumIntegerDigits 整数部の最小桁数 minimumFractionDigits 小数部の最小桁数 maximumFractionDigits 小数部の最大桁数 minimumSignificantDigits 最小有効桁数 maximumSignificantDigits 最大有効桁数 useGrouping 桁区切り等の使用有無
構文
- resolvedOptions ()
- なし
オプション オブジェクト
例
var nf = new Intl.NumberFormat();
console.log(nf.resolvedOptions());
// 出力:
// object {
// locale:"ja"
// maximumFractionDigits:3
// minimumFractionDigits:0
// minimumIntegerDigits:1
// numberingSystem:"latn"
// style:"decimal"
// useGrouping:true
// }
console.log(nf.format(12345)); // 出力:12,345
console.log(nf.format(12345.6789)); // 出力:12,345.679
関連
Intl.NumberFormat.supportedLocalesOf()【サポート ロケール取得】
メモ
- サポートされるロケールの配列を取得
構文
- Intl.NumberFormat.supportedLocalesOf( locales [, options ] )
- locales:BCP 47 の言語タグの文字列 または 配列
- options:オプション (localeMatcher【ロケールマッチングアルゴリズム】)
サポートされるロケールの配列