Intl.NumberFormat【国際化数値フォーマット】
new Intl.NumberFormat【コンストラクタ】
format【フォーマット関数】
formatRange【フォーマット (範囲)】
formatToParts【フォーマットパーツ取得】
formatRangeToParts【フォーマットパーツ取得 (範囲)】
resolvedOptions【オプション取得】
supportedLocalesOf【サポート ロケール取得】
new Intl.NumberFormat【コンストラクタ】
メモ
概要
- Intl.NumberFormat【国際化数値フォーマット】オブジェクトを生成
- format【フォーマット関数】で文字列に変換
- オプション の指定
- style【スタイル】:数値・通貨・パーセント・単位
- 追加オプション:数字(算用数字・全角数字・漢数字 等)・桁区切り 等
- ゼロパディング (オプション に以下を指定)
関連
外部リンク
- ECMA-402 (英語)
Intl.NumberFormat ([ locales [ , options ]]) ES2024 Intl (11) ES2023 Intl (10) ES2022 Intl (9) InitializeNumberFormat (numberFormat, locales, options) ES2024 Intl (11) ES2023 Intl (10) ES2022 Intl (9) IsSanctionedSimpleUnitIdentifier ( unitIdentifier ) ES2024 Intl (11) ES2023 Intl (10) ES2022 Intl (9) Table: Single units sanctioned for use in ECMAScript ES2024 Intl (11) ES2023 Intl (10) ES2022 Intl (9) Table: Rounding modes in Intl.NumberFormat ES2024 Intl (11) ES2023 Intl (10) ES2022 Intl (9) - RFC 5646:Tags for Identifying Languages (英語)
- RFC 4647:Matching of Language Tags (英語)
- Wikipedia:通貨コード (ISO 4217)
構文
new Intl.NumberFormat ([locales [, options]])
Intl.NumberFormat【国際化数値フォーマット】オブジェクト
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 (オプション) 実装依存
オプション | 値 (太字:デフォルト値) | 備考 |
---|---|---|
スタイル | ||
style | 'currency':通貨 (currency 必須) 'decimal':数値 'percent':パーセント 'unit':単位 (unit 必須) | スタイル |
通貨 (style='currency') で有効 | ||
currency (必須) | 通貨コード | 通貨コード (ISO 4217: Wikipedia) JPY・USD・EUR 等 |
currencyDisplay | 'code':ISO通貨コード (例:NZD) 'symbol':通貨シンボル (例:NZ$) 'narrowSymbol':縮小通貨シンボル (例:$) 'name':通貨名 (例:New Zealand dollars) | 通貨の表示方法 (style='currency'【通貨】のみ有効) |
currencySign | 'standard':標準形式〔例:-¥123,456〕 'accounting':会計形式〔例:(¥123,456)〕 | 通貨マイナス表現 (signDisplay≠'never' で有効) |
単位 (style='unit') で有効 | ||
unit (必須) | unit【単位】の値 参照 | 単位 Unicode Technical Standard #35 Section 3.11 のサブセット |
unitDisplay | 'short':短い形式 (例:min) 'narrow':縮小形式 (例:m) 'long':長い形式 (例:minutes) | 単位表現 |
共通 | ||
compactDisplay | 'short':短い形式 (例:M) 'long':長い形式 (例:million) | notation【表記法】の'compact'【数単位表記】の形式 (notation='compact'のみ有効) |
localeMatcher | 'lookup':Lookupアルゴリズム 'best fit':最適アルゴリズム | ロケールマッチングアルゴリズム |
maximumFractionDigits | minimumFractionDigits ~ 20 | 小数部の最大桁数 |
maximumSignificantDigits | minimumSignificantDigits ~ 21 | 最大有効桁数 |
minimumFractionDigits | 0 ~ 20 | 小数部の最小桁数 |
minimumIntegerDigits | 1 ~ 21 | 整数部の最小桁数 |
minimumSignificantDigits | 1 ~ 21 | 最小有効桁数 |
notation | 'standard':標準表記 'scientific':科学表記 (指数表記) 'engineering':工学表記 (10 3n となる指数表記) 'compact':数単位表記 (例:million・億) | 表記法 ('compact'【数単位表記】は compactDisplay で形式指定可) |
numberingSystem | Unicode 拡張 数値フォーマット 参照 | 数値システム |
roundingIncrement | 1 (デフォルト) ~ 5000 (有効:1, 2, 5, 10, 20, 25, 50, 100, 200, 250, 500, 1000, 2000, 2500, 5000) | 丸め増分 (参考:通貨金額) 各種ブラウザは未対応 (2023-6 時点) |
roundingMode | 'ceil': 'floor': 'expand': 'trunc': 'halfCeil': 'halfFloor': 'halfExpand': 'halfTrunc': 'halfEven': | 丸めモード 各種ブラウザは未対応 (2023-6 時点) |
roundingPriority | 'auto': 'morePrecision': 'lessPrecision': | 丸め優先 各種ブラウザは未対応 (2023-6 時点) |
signDisplay | 'auto':負数のみ表示 (-0 を含む) 'always':常時表示 'never':非表示 'exceptZero':ゼロ以外表示 'negative':負数のみ表示 (-0 を含まない) | 符号表示 |
trailingZeroDisplay | auto:指定桁数で表示 stripIfInteger:整数は小数点以下を非表示 (例:123.00 → 123) | 小数点以下のゼロ表示方法 Firefox 114.0.2 (2023-6-20) は未対応 |
useGrouping | 未満 true:使用 false:未使用 以降 各種ブラウザは未対応 (2023-6 時点) 'min2':2桁以上になるように桁区切り (notation【表記法】が'compact'【数単位表記】の場合、デフォルト) 'auto':使用 (ロケール考慮) 'always':使用 (ロケール無視) true:'always'と同等 false:未使用 | 桁区切りの使用 |
unit【単位】の値
'-per-' で結合可 (例:'kilometer-per-hour':km/h)
値 | 例 (一例) | ||
---|---|---|---|
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 | 年 |
例
const num = 1_234_567.89
// 数値
let numFormat = new Intl.NumberFormat();
console.log( numFormat.format(num) );
// 出力:1,234,567.89
// 通貨
numFormat = new Intl.NumberFormat('ja-JP', { style:'currency', currency:'JPY' });
console.log( numFormat.format(num) );
// 出力:¥1,234,568
numFormat = new Intl.NumberFormat('ja-JP', { style:'currency', currency:'JPY', currencyDisplay:'name' });
console.log( numFormat.format(num) );
// 出力:1,234,568円
numFormat = new Intl.NumberFormat('en-US', { style:'currency', currency:'USD' });
console.log( numFormat.format(num) );
// 出力:$1,234,567.89
numFormat = new Intl.NumberFormat('de-DE', { style:'currency', currency:'EUR' });
console.log( numFormat.format(num) );
// 出力:1.234.567,89 €
numFormat = new Intl.NumberFormat('fr-FR', { style:'currency', currency:'EUR' });
console.log( numFormat.format(num) );
// 出力:1 234 567,89 €
// パーセント
numFormat = new Intl.NumberFormat('ja-JP', { style:'percent' });
console.log( numFormat.format(0.50) );
// 出力:50%
// 全角数字
const format_zen = new Intl.NumberFormat(
'ja-JP',
{ numberingSystem: 'fullwide' }
);
console.log( format_zen.format(num) );
// 出力:1,234,567.89
// 漢数字 (拡張 数値フォーマット指定)
const format_kanji = new Intl.NumberFormat(
'ja-JP-u-nu-hanidec',
// { numberingSystem: 'hanidec' }
);
console.log( format_kanji.format(num) );
// 出力:一,二三四,五六七.八九
// ゼロパディング
numFormat = new Intl.NumberFormat(
'ja-JP',
{ useGrouping:false, minimumIntegerDigits:9, minimumFractionDigits:4 }
);
console.log(numFormat.format(num));
// 出力:001234567.8900
Intl.NumberFormat.prototype.format【フォーマット関数】
Intl.NumberFormat.prototype.formatRange【フォーマット (範囲)】
メモ
概要
- 値・値範囲をフォーマット文字列に変換
- フォーマット指定:new Intl.NumberFormat【コンストラクタ】
- format【フォーマット関数】はゲッターで別種類のオブジェクトからも利用可 (Array.map【配列変換生成】等)
関連
外部リンク
- ECMA-402 (英語)
get Intl.NumberFormat.prototype.format ES2024 Intl (11) ES2023 Intl (10) ES2022 Intl (9) Intl.NumberFormat.prototype.formatRange ( start, end ) ES2024 Intl (11) ES2023 Intl (10)
構文
get numberFormat.format ( value )
numberFormat.formatRange ( start, end )
format【フォーマット関数】フォーマット関数 (別種類のオブジェクトからも利用可)
formatRange【フォーマット (範囲)】フォーマット文字列
value値 (数値変換)
start開始値 (数値変換)
end終了値 (数値変換)
TypeError start (開始値)・end (終了値)のどちらかが省略
RangeError start (開始値)・end (終了値)のどちらかが NaN または 数値変換不可
※ start (開始値)>end (終了値)も可
例:Array.map【配列変換生成】利用
const nums = [ 1234, 1234567, 1234567890, 1234567.89 ];
const numFormat = new Intl.NumberFormat();
console.log( nums.map( formatter.format ) );
// 出力:(4) ['1,234', '1,234,567', '1,234,567,890', '1,234,567.89']
例:数値 (style='decimal')
const num = 1_234_567.89;
const locales = [ 'ja-JP', 'en-US', 'de-DE', 'fr-FR' ];
const ranges = [ [123, 456], [789, 789] ];
for (const locale of locales) {
const numFormat = new Intl.NumberFormat(locale);
const str = numFormat.format( num );
const str_0 = numFormat.formatRange( ranges[0][0], ranges[0][1] );
const str_1 = numFormat.formatRange( ranges[1][0], ranges[1][1] );
console.log( `${str} / ${str_0} / ${str_1}' ('${locale}')` );
}
// 出力:
// 1,234,567.89 / 123~456 / 約789' ('ja-JP')
// 1,234,567.89 / 123–456 / ~789' ('en-US')
// 1.234.567,89 / 123–456 / ≈789' ('de-DE')
// 1 234 567,89 / 123–456 / ≃789' ('fr-FR')
// 表記法
const value = 12_345_678_901;
notations = ['standard', 'scientific', 'engineering', 'compact'];
for (const locale of locales) {
const format_std = new Intl.NumberFormat(
locale,
{ notation: 'standard'} // 標準表記
);
const str_std = format_std.format( value );
const format_sci = new Intl.NumberFormat(
locale,
{ notation: 'scientific'} // 科学表記 (指数表記)
);
const str_sci = format_sci.format( value );
const format_eng = new Intl.NumberFormat(
locale,
{ notation: 'engineering'} // 工学表記 (10^3n となる指数表記)
);
const str_eng = format_eng.format( value );
const format_short = new Intl.NumberFormat(
locale,
{ notation: 'compact', compactDisplay: 'short'} // 数単位表記 (短い形式)
);
const str_short = format_short.format( value );
const format_long = new Intl.NumberFormat(
locale,
{ notation: 'compact', compactDisplay: 'long'} // 数単位表記 (長い形式)
);
const str_long = format_long.format( value );
console.log( `${str_std} / ${str_sci} / ${str_eng} / ${str_short} / ${str_long}' ('${locale}')` );
}
// 出力:
// 12,345,678,901 / 1.235E10 / 12.346E9 / 123億 / 123億' ('ja-JP')
// 12,345,678,901 / 1.235E10 / 12.346E9 / 12B / 12 billion' ('en-US')
// 12.345.678.901 / 1,235E10 / 12,346E9 / 12 Mrd. / 12 Milliarden' ('de-DE')
// 12 345 678 901 / 1,235E10 / 12,346E9 / 12 Md / 12 milliards' ('fr-FR')
例:通貨 (style='currency')
const num = 1_234_567.89;
const range = [1234.56, 7890.12];
const locales = [ ['ja-JP', 'JPY'], ['en-US', 'USD'], ['de-DE', 'EUR'], ['en-NZ', 'NZD'], ['en-US', 'NZD'] ];
const displays = [ 'code', 'symbol', 'narrowSymbol', 'name' ];
for (const locale of locales) {
const cur = locale[1];
console.log( `\n'${locale[0]}', '${cur}'` );
for (const display of displays) {
const numFormat = new Intl.NumberFormat(
locale[0],
{ style: 'currency', currency: cur, currencyDisplay: display }
);
const str = numFormat.format( num );
console.log( `${str} ('${display}')` );
}
const numFormat = new Intl.NumberFormat(
locale[0],
{ style: 'currency', currency: cur }
);
console.log( numFormat.formatRange(range[0], range[1]) );
}
// 出力:
// 'ja-JP', 'JPY'
// JPY 1,234,568 ('code')
// ¥1,234,568 ('symbol')
// ¥1,234,568 ('narrowSymbol')
// 1,234,568円 ('name')
// ¥1,235 ~ ¥7,890
//
// 'en-US', 'USD'
// USD 1,234,567.89 ('code')
// $1,234,567.89 ('symbol')
// $1,234,567.89 ('narrowSymbol')
// 1,234,567.89 US dollars ('name')
// $1,234.56 – $7,890.12
//
// 'de-DE', 'EUR'
// 1.234.567,89 EUR ('code')
// 1.234.567,89 € ('symbol')
// 1.234.567,89 € ('narrowSymbol')
// 1.234.567,89 Euro ('name')
// 1.234,56–7.890,12 €
//
// 'en-NZ', 'NZD'
// NZD 1,234,567.89 ('code')
// $1,234,567.89 ('symbol')
// $1,234,567.89 ('narrowSymbol')
// 1,234,567.89 New Zealand dollars ('name')
// $1,234.56 – $7,890.12
//
// 'en-US', 'NZD'
// NZD 1,234,567.89 ('code')
// NZ$1,234,567.89 ('symbol')
// $1,234,567.89 ('narrowSymbol')
// 1,234,567.89 New Zealand dollars ('name')
// NZ$1,234.56–7,890.12
例:パーセント (style='percent')
const locales = [ 'ja-JP', 'en-US', 'de-DE', 'fr-FR' ];
const num = 0.5;
const ranges = [ [0.6, 0.7], [0.9, 1] ];
for (const locale of locales) {
const formatter = new Intl.NumberFormat(
locale,
{ style: 'percent', minimumFractionDigits: 2 }
);
const str = formatter.format( num );
const str_0 = formatter.formatRange( ranges[0][0], ranges[0][1] );
const str_1 = formatter.formatRange( ranges[1][0], ranges[1][1] );
console.log( `${str} / ${str_0} / ${str_1}' ('${locale}')` );
}
// 出力:
// 50.00% / 60.00% ~ 70.00% / 90.00% ~ 100.00%' ('ja-JP')
// 50.00% / 60.00% – 70.00% / 90.00% – 100.00%' ('en-US')
// 50,00 % / 60,00–70,00 % / 90,00–100,00 %' ('de-DE')
// 50,00 % / 60,00–70,00 % / 90,00–100,00 %' ('fr-FR')
例:単位 (style='unit')
const locales = [ 'ja-JP', 'en-US' ];
const units = [ 'kilogram', 'meter-per-hour', 'minute', 'second' ];
const displays = [ 'short', 'narrow', 'long' ];
const num = 123.45;
const ranges = [[50, 70], [60, 60]];
for (const locale of locales) {
console.log( `\n'${locale}'` );
for (const unt of units) {
console.log( `'${unt}'` );
for (const display of displays) {
const formatter = new Intl.NumberFormat(
locale,
{ style: 'unit', unit: unt, unitDisplay: display }
);
const str = formatter.format( num );
const str_0 = formatter.formatRange( ranges[0][0], ranges[0][1] );
const str_1 = formatter.formatRange( ranges[1][0], ranges[1][1] );
console.log( `${str} / ${str_0} / ${str_1} ('${display}')` );
}
}
}
// 出力:
// 'ja-JP'
// 'kilogram'
// 123.45 kg / 50~70 kg / 約60 kg ('short')
// 123.45kg / 50~70kg / 約60kg ('narrow')
// 123.45 キログラム / 50~70 キログラム / 約60 キログラム ('long')
// 'meter-per-hour'
// 123.45 m/時間 / 50~70 m/時間 / 約60 m/時間 ('short')
// 123.45m/時間 / 50~70m/時間 / 約60m/時間 ('narrow')
// 123.45 メートル/時間 / 50~70 メートル/時間 / 約60 メートル/時間 ('long')
// 'minute'
// 123.45 分 / 50~70 分 / 約60 分 ('short')
// 123.45m / 50~70m / 約60m ('narrow')
// 123.45 分 / 50~70 分 / 約60 分 ('long')
// 'second'
// 123.45 秒 / 50~70 秒 / 約60 秒 ('short')
// 123.45s / 50~70s / 約60s ('narrow')
// 123.45 秒 / 50~70 秒 / 約60 秒 ('long')
//
// 'en-US'
// 'kilogram'
// 123.45 kg / 50–70 kg / ~60 kg ('short')
// 123.45kg / 50–70kg / ~60kg ('narrow')
// 123.45 kilograms / 50–70 kilograms / ~60 kilograms ('long')
// 'meter-per-hour'
// 123.45 m/h / 50–70 m/h / ~60 m/h ('short')
// 123.45m/h / 50–70m/h / ~60m/h ('narrow')
// 123.45 meters per hour / 50–70 meters per hour / ~60 meters per hour ('long')
// 'minute'
// 123.45 min / 50–70 min / ~60 min ('short')
// 123.45m / 50–70m / ~60m ('narrow')
// 123.45 minutes / 50–70 minutes / ~60 minutes ('long')
// 'second'
// 123.45 sec / 50–70 sec / ~60 sec ('short')
// 123.45s / 50–70s / ~60s ('narrow')
// 123.45 seconds / 50–70 seconds / ~60 seconds ('long')
Intl.NumberFormat.prototype.formatToParts【フォーマットパーツ取得】
Intl.NumberFormat.prototype.formatRangeToParts【フォーマットパーツ取得 (範囲)】
メモ
概要
- フォーマットされた数値・数値範囲をパーツに分解
- パーツを編集し組み合わせることで、独自フォーマットが可能
関連
外部リンク
- ECMA-402 (英語)
Intl.NumberFormat.prototype.formatToParts ( value ) ES2024 Intl (11) ES2023 Intl (10) ES2022 Intl (9) Intl.NumberFormat.prototype.formatRangeToParts ( start, end ) ES2024 Intl (11) ES2023 Intl (10) ES2022 Intl (9)
構文
numberFormat.formatToParts ( value )
numberFormat.formatRangeToParts ( start, end )
(Array)フォーマットのパーツ
value数値
start開始値
end終了値
例
const num = 1_234_567.89;
const range = [1234, 5678];
let numFormat = new Intl.NumberFormat();
console.log( numFormat.format(num) );
// 出力:1,234,567.89
console.log( numFormat.formatToParts(num) );
// 出力:
// (7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}]
// 0: {type: 'integer', value: '1'}
// 1: {type: 'group', value: ','}
// 2: {type: 'integer', value: '234'}
// 3: {type: 'group', value: ','}
// 4: {type: 'integer', value: '567'}
// 5: {type: 'decimal', value: '.'}
// 6: {type: 'fraction', value: '89'}
// length: 7
// [[Prototype]]: Array(0)
console.log( numFormat.formatRange(...range) );
// 出力:1,234~5,678
console.log( numFormat.formatRangeToParts(range[0], range[1]) );
// 出力:
// (7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}]
// 0: {type: 'integer', value: '1', source: 'startRange'}
// 1: {type: 'group', value: ',', source: 'startRange'}
// 2: {type: 'integer', value: '234', source: 'startRange'}
// 3: {type: 'literal', value: '~', source: 'shared'}
// 4: {type: 'integer', value: '5', source: 'endRange'}
// 5: {type: 'group', value: ',', source: 'endRange'}
// 6: {type: 'integer', value: '678', source: 'endRange'}
// length: 7
// [[Prototype]]: Array(0)
numFormat = new Intl.NumberFormat('en-US', {style: 'currency', currency: 'NZD'});
console.log( numFormat.format(num) );
// 出力:NZ$1,234,567.89
console.log( numFormat.formatToParts(num) );
// 出力:
// (8) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
// 0: {type: 'currency', value: 'NZ$'}
// 1: {type: 'integer', value: '1'}
// 2: {type: 'group', value: ','}
// 3: {type: 'integer', value: '234'}
// 4: {type: 'group', value: ','}
// 5: {type: 'integer', value: '567'}
// 6: {type: 'decimal', value: '.'}
// 7: {type: 'fraction', value: '89'}
// length: 8
// [[Prototype]]: Array(0)
console.log( numFormat.formatRange(...range) );
// 出力:NZ$1,234.00–5,678.00
console.log( numFormat.formatRangeToParts(range[0], range[1]) );
// 出力:
// (12) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
// 0: {type: 'currency', value: 'NZ$', source: 'shared'}
// 1: {type: 'integer', value: '1', source: 'startRange'}
// 2: {type: 'group', value: ',', source: 'startRange'}
// 3: {type: 'integer', value: '234', source: 'startRange'}
// 4: {type: 'decimal', value: '.', source: 'startRange'}
// 5: {type: 'fraction', value: '00', source: 'startRange'}
// 6: {type: 'literal', value: '–', source: 'shared'}
// 7: {type: 'integer', value: '5', source: 'endRange'}
// 8: {type: 'group', value: ',', source: 'endRange'}
// 9: {type: 'integer', value: '678', source: 'endRange'}
// 10: {type: 'decimal', value: '.', source: 'endRange'}
// 11: {type: 'fraction', value: '00', source: 'endRange'}
// length: 12
// [[Prototype]]: Array(0)
numFormat = new Intl.NumberFormat('en-US', {style: 'unit', unit: 'megabyte'});
console.log( numFormat.format(num) );
// 出力:1,234,567.89 MB
console.log( numFormat.formatToParts(num) );
// 出力:
// (9) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
// 0: {type: 'integer', value: '1'}
// 1: {type: 'group', value: ','}
// 2: {type: 'integer', value: '234'}
// 3: {type: 'group', value: ','}
// 4: {type: 'integer', value: '567'}
// 5: {type: 'decimal', value: '.'}
// 6: {type: 'fraction', value: '89'}
// 7: {type: 'literal', value: ' '}
// 8: {type: 'unit', value: 'MB'}
// length: 9
// [[Prototype]]: Array(0)
console.log( numFormat.formatRange(...range) );
// 出力:1,234–5,678 MB
console.log( numFormat.formatRangeToParts(range[0], range[1]) );
// 出力:
// (9) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
// 0: {type: 'integer', value: '1', source: 'startRange'}
// 1: {type: 'group', value: ',', source: 'startRange'}
// 2: {type: 'integer', value: '234', source: 'startRange'}
// 3: {type: 'literal', value: '–', source: 'shared'}
// 4: {type: 'integer', value: '5', source: 'endRange'}
// 5: {type: 'group', value: ',', source: 'endRange'}
// 6: {type: 'integer', value: '678', source: 'endRange'}
// 7: {type: 'literal', value: ' ', source: 'shared'}
// 8: {type: 'unit', value: 'MB', source: 'shared'}
// length: 9
// [[Prototype]]: Array(0)
Intl.NumberFormat.prototype.resolvedOptions【オプション取得】
メモ
概要
- 以下のプロパティを持ったオプション オブジェクトを取得 (new Intl.NumberFormat【コンストラクタ】も参照) (デフォルト値がない未指定のプロパティは存在しない)
プロパティ 備考 locale ロケール numberingSystem Unicode 拡張 数値フォーマット style スタイル currency 通貨コード currencyDisplay 通貨の表示方法 currencySign 通貨マイナス表現 unit 単位 unitDisplay 単位表現 minimumIntegerDigits 整数部の最小桁数 minimumFractionDigits 小数部の最小桁数 maximumFractionDigits 小数部の最大桁数 minimumSignificantDigits 最小有効桁数 maximumSignificantDigits 最大有効桁数 useGrouping 桁区切りの使用 notation 表記法 compactDisplay 数単位表記の形式 signDisplay 符号表示
関連
外部リンク
- ECMA-402 (英語)
Intl.NumberFormat.prototype.resolvedOptions () ES2024 Intl (11) ES2023 Intl (10) ES2022 Intl (9)
構文
numberFormat.resolvedOptions ()
オプション オブジェクト
例
const numFormat = new Intl.NumberFormat();
console.log( numFormat.resolvedOptions() );
// 出力:
// {locale: 'ja', numberingSystem: 'latn', style: 'decimal', minimumIntegerDigits: 1, minimumFractionDigits: 0, …}
// locale: "ja"
// maximumFractionDigits: 3
// minimumFractionDigits: 0
// minimumIntegerDigits: 1
// notation: "standard"
// numberingSystem: "latn"
// roundingIncrement: 1
// roundingMode: "halfExpand"
// roundingPriority: "auto"
// signDisplay: "auto"
// style: "decimal"
// trailingZeroDisplay: "auto"
// useGrouping: "auto"
// [[Prototype]]: Object
console.log( numFormat.format(12345) );
// 出力:12,345
console.log( numFormat.format(12345.6789) );
// 出力:12,345.679
Intl.NumberFormat.supportedLocalesOf【サポート ロケール取得】
メモ
概要
- 指定したロケールから、サポート対象のロケールを取得
関連
外部リンク
- ECMA-402 (英語)
Intl.NumberFormat.supportedLocalesOf (locales [, options ]) ES2024 Intl (11) ES2023 Intl (10) ES2022 Intl (9)
構文
Intl.NumberFormat.supportedLocalesOf( locales [, options ] )
サポートされるロケールの配列
localesBCP 47 の言語タグの文字列 または その配列
optionsオプション (localeMatcher【ロケールマッチングアルゴリズム】)
※ new Intl.NumberFormat【コンストラクタ】参照
例
console.log( Intl.NumberFormat.supportedLocalesOf('ja') );
// 出力例:['ja']
console.log( Intl.NumberFormat.supportedLocalesOf('ng') );
// 出力例:[]
let locales = ['ng', 'ja', 'ja-JP', 'en', 'en-US', 'en-GB'];
console.log( Intl.NumberFormat.supportedLocalesOf(locales) );
// 出力例:(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) );
// 出力例:(5) ['ja', 'ja-JP', 'en', 'en-US', 'en-GB']
console.log( Intl.NumberFormat.supportedLocalesOf(locales, { localeMatcher: 'lookup' }) );
// 出力例:(5) ['ja', 'ja-JP', 'en', 'en-US', 'en-GB']