Intl.NumberFormat【国際化数値フォーマット】
new Intl.NumberFormat()【コンストラクタ】
format【フォーマット関数】
formatToParts【フォーマットパーツ取得】
resolvedOptions()【オプション取得】
supportedLocalesOf()【サポート ロケール取得】

new Intl.NumberFormat()【コンストラクタ】

メモ

構文

new Intl.NumberFormat ([ locales [, options ]])

Intl.NumberFormat【国際化数値フォーマット】 オブジェクト
localesBCP 47 の言語タグの文字列 または 配列 下記 参照 (省略:デフォルトのロケール)
localesロケール (BCP 47 の言語タグ等 + 拡張フォーマット)〔実装依存〕
     省略:デフォルトのロケール〔実装依存〕
     文字列:1ロケール指定
     文字列の配列:複数指定可 (適切なロケールを自動選択)
optionsオプション (下記 参照)

locales (ロケール) 実装依存

BCP 47 の言語タグ (一例)
備考
ja日本語
ja-JP日本語 (日本)
en-US英語 (アメリカ)
en-GB英語 (イギリス)
de-DEドイツ語 (ドイツ)
fr-FRフランス語 (フランス)
ISO 639-1・639-2 (言語コード) 一例
ISO 639-1ISO 639-2ISO 639-3備考
jajpnjpn日本語
enengeng英語
dedeu
ger
deuドイツ語
frfra
fre
fraフランス語

Unicode 拡張 数値フォーマット 実装依存


(-u-nu-○)
説明コード
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 ~ 9U+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+4E5D
khmrクメール数字០ ~ ៩U+17E0 ~ U+17E9
kndaカンナダ数字೦ ~ ೯U+0CE6 ~ U+0CEF
laooラオ数字໐ ~ ໙U+0ED0 ~ U+0ED9
latn算用数字 (アラビア数字) 0 ~ 9U+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 必須)
'unit':単位 (unit 必須)
スタイル
currency通貨コード 通貨コード (ISO 4217: Wikipedia)
JPY・USD・EUR 等
(style='currency'【通貨】のみ有効)
currencyDisplay 'code':ISO通貨コード (例:NZD)
'symbol':通貨シンボル (例:NZ$)
'narrowSymbol':縮小通貨シンボル (例:$)
'name':通貨名 (例:New Zealand dollars)
通貨の表示方法
(style='currency'【通貨】のみ有効)
currencySign 'standard':標準形式 (例:-¥123,456)
'accounting':会計形式 (例:(¥123,456))
通貨マイナス表現
(style='currency'【通貨】かつ signDisplay≠'never' のみ有効)
unit unit【単位】の値 参照 Unicode Technical Standard #35 Section 3.11 のサブセット
(style='unit'【単位】のみ有効)
unitDisplay 'short':短い形式 (例:min)
'narrow':縮小形式 (例:m)
'long':長い形式 (例:minutes)
単位表現
(style='unit'【単位】のみ有効)
minimumIntegerDigits1 ~ 21整数部の最小桁数
minimumFractionDigits0 ~ 20小数部の最小桁数
maximumFractionDigitsminimumFractionDigits ~ 20小数部の最大桁数
minimumSignificantDigits1 ~ 21最小有効桁数
maximumSignificantDigitsminimumSignificantDigits ~ 21最大有効桁数
useGroupingtrue:使用
false:未使用
桁区切り等の使用有無
notation 'standard':標準表記
'scientific':科学表記 (指数表記)
'engineering':工学表記 (10 3n となる指数表記)
'compact':数単位表記 (例:million・億)
表記法
('compact'【数単位表記】は compactDisplay で形式指定可)
compactDisplay 'short':短い形式 (例:M)
'long':長い形式 (例:million)
notation【表記法】の'compact'【数単位表記】の形式
(notation='compact'のみ有効)
signDisplay 'auto':負数のみ表示
'always':常時表示
'never':非表示
'exceptZero':ゼロ以外表示
符号表示

unit【単位】の値

例 (一例)
acreacacresエーカー
bitbbitビット
byteBbyteバイト
celsius°Cdegrees Celsius摂氏 ~ 度
centimetercmcentimetersセンチメートル
dayddays
degree°deg
fahrenheit°Fdegrees Fahrenheit華氏 ~ 度
fluid-ouncefl ozfluid ounces液量オンス
footftフィート
gallongalgallonsガロン
gigabitGbgigabitsギガビット
gigabyteGBgigabytesギガバイト
gramggramsグラム
hectarehahectaresヘクタール
hourhhours時間
inchinchesインチ
kilobitkbkilobitsキロビット
kilobytekBkilobytesキロバイト
kilogramkgkilogramsキログラム
kilometerkmkilometersキロメートル
literLlitersリットル
megabitMbmegabitsメガビット
megabyteMBmegabytesメガバイト
metermmetersメートル
milemimilesマイル
mile-scandinaviansmimiles-scandinavianスカンジナビアマイル
millilitermLmillilitersミリリットル
millimetermmmillimetersミリメートル
millisecondmsmillisecondsミリ秒
minutemmin
monthmmonthsか月
ounceozouncesオンス
percent%percentパーセント
petabytePBpetabytesペタバイト
poundlbpoundsポンド
secondssec
stoneststonesストーン
terabitTbterabitsテラビット
terabyteTBterabytesテラバイト
weekwweeks週間
yardydyardsヤード
yearyyears

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

Intl.NumberFormat.prototype.format【フォーマット関数】

メモ

フォーマット関数構文

numberFormat.format ( value )

フォーマット文字列
value数値

new Intl.NumberFormat () 【コンストラクタ】の例を参照

// Array.map ()【配列変換生成】利用
const nums = [ 123, 123456, 123456789, 123456.789 ];
let formatter = new Intl.NumberFormat();
console.log(nums.map(formatter.format));
// 出力:["123", "123,456", "123,456,789", "123,456.789"]

Intl.NumberFormat.prototype.formatToParts【フォーマットパーツ取得】

メモ

構文

numberFormat.formatToParts ( value ) 

 (Array)フォーマットのパーツ
value数値

const num = 123456.789;

let formatter = new Intl.NumberFormat();
console.log(formatter.format(num));
// 出力:123,456.789
console.log(formatter.formatToParts(num));
// 出力:
// Array(5)
// 0: {type: "integer", value: "123"}
// 1: {type: "group", value: ","}
// 2: {type: "integer", value: "456"}
// 3: {type: "decimal", value: "."}
// 4: {type: "fraction", value: "789"}

let fCurrency = new Intl.NumberFormat('en-US', {style: 'currency', currency: 'NZD'});
console.log(fCurrency.format(num));
// 出力:NZ$123,456.79
console.log(fCurrency.formatToParts(num));
// 出力:
// Array(6)
// 0: {type: "currency", value: "NZ$"}
// 1: {type: "integer", value: "123"}
// 2: {type: "group", value: ","}
// 3: {type: "integer", value: "456"}
// 4: {type: "decimal", value: "."}
// 5: {type: "fraction", value: "79"}

let fUnit = new Intl.NumberFormat('en-US', {style: 'unit', unit: 'megabyte'});
console.log(fUnit.format(num));
// 出力:123,456.789 MB
console.log(fUnit.formatToParts(num));
// 出力:
// Array(7)
// 0: {type: "integer", value: "123"}
// 1: {type: "group", value: ","}
// 2: {type: "integer", value: "456"}
// 3: {type: "decimal", value: "."}
// 4: {type: "fraction", value: "789"}
// 5: {type: "literal", value: " "}
// 6: {type: "unit", value: "MB"}

Intl.NumberFormat.prototype.resolvedOptions()【オプション取得】

メモ

  • 以下のプロパティを持ったオプション オブジェクトを取得 (デフォルト値がない未指定のプロパティは存在しない)
    (new Intl.NumberFormat() 【コンストラクタ】も参照)
    プロパティ説明
    localeBCP47言語タグ
    numberingSystemUnicode 拡張 数値フォーマット
    styleスタイル
    currency通貨コード
    currencyDisplay通貨の表示方法
    currencySign 通貨マイナス表現
    unit 単位
    unitDisplay
    minimumIntegerDigits整数部の最小桁数
    minimumFractionDigits小数部の最小桁数
    maximumFractionDigits小数部の最大桁数
    minimumSignificantDigits最小有効桁数
    maximumSignificantDigits最大有効桁数
    useGrouping桁区切り等の使用有無
    notation 表記法
    compactDisplay 数単位表記の形式
    signDisplay 符号表示
  • 関連
  • 外部リンク (英語)
    Intl.NumberFormat.prototype.resolvedOptions ()
    ES2022 Intl (9) ES2021 Intl (8) ES2020 Intl (7)

構文

numberFormat.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 ] ) 

サポートされるロケールの配列
localesBCP 47 の言語タグの文字列 または 配列
optionsオプション (localeMatcher【ロケールマッチングアルゴリズム】)
※ new Intl.NumberFormat () 【コンストラクタ】を参照

console.log(Intl.NumberFormat.supportedLocalesOf('ja'));
// 出力例:Array [ "ja" ]

console.log(Intl.NumberFormat.supportedLocalesOf('ng'));
// 出力例:Array []

let locales = ['ng', 'ja', 'ja-JP', 'en', 'en-US', 'en-GB'];
console.log(Intl.NumberFormat.supportedLocalesOf(locales));
// 出力例:Array(5) [ "ja", "ja-JP", "en", "en-US", "en-GB" ]

locales = ['ng', 'jpn', 'jpn-JP', 'eng', 'eng-US', 'eng-GB'];
console.log(Intl.NumberFormat.supportedLocalesOf(locales));
// 出力例:Array(5) [ "ja", "ja-JP", "en", "en-US", "en-GB" ]

console.log(Intl.NumberFormat.supportedLocalesOf(locales, { localeMatcher: 'lookup' }));
// 出力例:Array(5) [ "ja", "ja-JP", "en", "en-US", "en-GB" ]