Intl.NumberFormat【国際化数値フォーマット】
new Intl.NumberFormat()【コンストラクタ】
format【フォーマット関数】
formatToParts【フォーマットパーツ取得】
resolvedOptions()【オプション取得】
supportedLocalesOf()【サポート ロケール取得】
new Intl.NumberFormat()【コンストラクタ】
メモ
- Intl.NumberFormat【国際化数値フォーマット】 オブジェクトを生成
- format【フォーマット関数】で文字列に変換
- 数字・通貨・桁数 等の指定が可能
- ゼロパディング:オプションに以下を指定
- useGrouping【桁区切り等の使用有無】:false
- minimumIntegerDigits【整数部の最小桁数】:桁数
- 関連
- 外部リンク
Intl.NumberFormat ([ locales [ , options ]]) (英語) ES2022 Intl (9) ES2021 Intl (8) ES2020 Intl (7) InitializeNumberFormat (numberFormat, locales, options) (英語) ES2022 Intl (9) ES2021 Intl (8) ES2020 Intl (7) IsSanctionedSimpleUnitIdentifier ( unitIdentifier ) (英語) ES2022 Intl (9) ES2021 Intl (8) ES2020 Intl (7)
構文
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-1 | ISO 639-2 | ISO 639-3 | 備考 |
---|---|---|---|
ja | jpn | jpn | 日本語 |
en | eng | eng | 英語 |
de | deu ger | deu | ドイツ語 |
fr | fra 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 ~ 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+4E5D |
khmr | クメール数字 | ០ ~ ៩ | 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 必須) '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'【単位】のみ有効) |
minimumIntegerDigits | 1 ~ 21 | 整数部の最小桁数 |
minimumFractionDigits | 0 ~ 20 | 小数部の最小桁数 |
maximumFractionDigits | minimumFractionDigits ~ 20 | 小数部の最大桁数 |
minimumSignificantDigits | 1 ~ 21 | 最小有効桁数 |
maximumSignificantDigits | minimumSignificantDigits ~ 21 | 最大有効桁数 |
useGrouping | true:使用 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【単位】の値
値 | 例 (一例) | ||
---|---|---|---|
acre | ac | acres | エーカー |
bit | b | bit | ビット |
byte | B | byte | バイト |
celsius | °C | degrees Celsius | 摂氏 ~ 度 |
centimeter | cm | centimeters | センチメートル |
day | d | days | 日 |
degree | ° | deg | 度 |
fahrenheit | °F | degrees Fahrenheit | 華氏 ~ 度 |
fluid-ounce | fl oz | fluid ounces | 液量オンス |
foot | ′ | ft | フィート |
gallon | gal | gallons | ガロン |
gigabit | Gb | gigabits | ギガビット |
gigabyte | GB | gigabytes | ギガバイト |
gram | g | grams | グラム |
hectare | ha | hectares | ヘクタール |
hour | h | hours | 時間 |
inch | ″ | inches | インチ |
kilobit | kb | kilobits | キロビット |
kilobyte | kB | kilobytes | キロバイト |
kilogram | kg | kilograms | キログラム |
kilometer | km | kilometers | キロメートル |
liter | L | liters | リットル |
megabit | Mb | megabits | メガビット |
megabyte | MB | megabytes | メガバイト |
meter | m | meters | メートル |
mile | mi | miles | マイル |
mile-scandinavian | smi | miles-scandinavian | スカンジナビアマイル |
milliliter | mL | milliliters | ミリリットル |
millimeter | mm | millimeters | ミリメートル |
millisecond | ms | milliseconds | ミリ秒 |
minute | m | min | 分 |
month | m | months | か月 |
ounce | oz | ounces | オンス |
percent | % | percent | パーセント |
petabyte | PB | petabytes | ペタバイト |
pound | lb | pounds | ポンド |
second | s | sec | 秒 |
stone | st | stones | ストーン |
terabit | Tb | terabits | テラビット |
terabyte | TB | terabytes | テラバイト |
week | w | weeks | 週間 |
yard | yd | yards | ヤード |
year | y | years | 年 |
例
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【フォーマット関数】
メモ
- フォーマット関数を返却
- フォーマット指定:new Intl.NumberFormat() 【コンストラクタ】
- Array.map ()【配列変換生成】等の利用可
- 関連
- 外部リンク (英語)
get Intl.NumberFormat.prototype.format ES2022 Intl (9) ES2021 Intl (8) ES2020 Intl (7)
フォーマット関数構文
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【フォーマットパーツ取得】
メモ
- フォーマットされた数値をパーツに分解
- パーツを編集し組み合わせることで、独自フォーマットが可能
- 関連
- 外部リンク (英語)
Intl.NumberFormat.prototype.formatToParts ( value ) ES2022 Intl (9) ES2021 Intl (8) ES2020 Intl (7)
構文
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() 【コンストラクタ】も参照)プロパティ 説明 locale BCP47言語タグ numberingSystem Unicode 拡張 数値フォーマット 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 ]) ES2022 Intl (9) ES2021 Intl (8) ES2020 Intl (7)
構文
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" ]