toExponential【文字列変換 (指数形式)】
toFixed【文字列変換 (固定小数点形式)】
toLocaleString【文字列変換 (ロケール)】
toPrecision【文字列変換 (有効桁数 指定)】
toString【文字列変換 (基数指定可)】
Number.prototype.toExponential【文字列変換 (指数形式)】
メモ
概要
- 指数形式の文字列に変換
- 文字列 ⇔ 数値 については、Number【数値】オブジェクトのメモ 参照
外部リンク
- ECMA-262 (英語)
Number.prototype.toExponential ( fractionDigits ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
number.toExponential( fractionDigits )
変換文字列 (指数形式)
fractionDigits 小数部桁数 (0~20)
RangeError 例外 fractionDigits (小数部桁数) が 0~20 の整数値に変換不可
例
var num;
num = 0.000123456789;
console.log(num.toExponential(5)); // 出力:1.23457e-4
num = 1.23456789;
console.log(num.toExponential(5)); // 出力:1.23457e+0
num = 1234.56789;
console.log(num.toExponential(5)); // 出力:1.23457e+3
num = 123456789;
console.log(num.toExponential(5)); // 出力:1.23457e+8
num = 0.000123;
console.log(num.toExponential(5)); // 出力:1.23000e-4
num = 123;
console.log(num.toExponential(5)); // 出力:1.23000e+2
num = 1234.56789;
console.log(num.toExponential(21)); // RangeError 例外 (範囲外の小数部桁数)
Number.prototype.toFixed【文字列変換 (固定小数点形式)】
メモ
概要
- 固定小数点形式の文字列に変換
- 文字列 ⇔ 数値 については、Number【数値】オブジェクトのメモ 参照
外部リンク
- ECMA-262 (英語)
Number.prototype.toFixed ( fractionDigits ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
number.toFixed( fractionDigits )
変換文字列 (固定小数点形式)
fractionDigits 小数部桁数 (0~20) 実装は拡張可
RangeError 例外 fractionDigits (小数部桁数)が 0~20 の整数値に変換不可
例
var num;
num = 0.000123456789;
console.log(num.toFixed(5)); // 出力:0.00012
num = 1.23456789;
console.log(num.toFixed(5)); // 出力:1.23457
num = 1234.56789;
console.log(num.toFixed(5)); // 出力:1234.56789
num = -1234.56789;
console.log(num.toFixed(5)); // 出力:-1234.56789
num = 123456789;
console.log(num.toFixed(5)); // 出力:123456789.00000
num = 0.000123;
console.log(num.toFixed(5)); // 出力:0.00012
num = 123;
console.log(num.toFixed(5)); // 出力:123.00000
num = 1234.56789;
console.log(num.toFixed(21)); // RangeError 例外 (範囲外の小数部桁数)
Number.prototype.toLocaleString【文字列変換 (ロケール)】
メモ
概要
- ロケール文字列に変換 (実装依存:3桁区切り)
- 文字列 ⇔ 数値 については、Number【数値】オブジェクトのメモ 参照
関連
外部リンク
- ECMA-262 (英語)
Number.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
number.toLocaleString( )
number.toLocaleString( [ reserved1[, reserved2]] )
number.toLocaleString( [ locales[, options]] )
変換文字列 (ロケール) 実装依存
reserved1 リザーブ1
reserved2 リザーブ2
locales BCP 47 の言語タグの文字列 または その配列 (省略:デフォルトのロケール)
options オプション
参照:new Intl.NumberFormat【国際化数値フォーマット コンストラクタ】
例
var num = 123456789;
console.log(num.toLocaleString());
// 出力:123,456,789
console.log(num.toLocaleString("ja-JP", { style:'currency', currency:'JPY' }));
// 出力:¥123,456,789
Number.prototype.toPrecision【文字列変換 (有効桁数 指定)】
メモ
概要
- 指定した有効桁数の文字列に変換
- 文字列 ⇔ 数値 については、Number【数値】オブジェクトのメモ 参照
外部リンク
- ECMA-262 (英語)
Number.prototype.toPrecision ( precision ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
number.toPrecision( precision )
変換文字列 (指定した有効桁数)
precision 有効桁数 (2~21)
RangeError 例外 precision (有効桁数)が 2~21 の整数値に変換不可
例
var num;
num = 0.000123456789;
console.log(num.toPrecision(5)); // 出力:0.00012346
num = 1.23456789;
console.log(num.toPrecision(5)); // 出力:1.2346
num = 1234.56789;
console.log(num.toPrecision(5)); // 出力:1234.6
num = 123456789;
console.log(num.toPrecision(5)); // 出力:1.2346e+8
num = 0.000123;
console.log(num.toPrecision(5)); // 出力:0.00012300
num = 123;
console.log(num.toPrecision(5)); // 出力:123.00
num = 1234.56789;
console.log(num.toPrecision(22)); // RangeError 例外 (範囲外の有効桁数)
Number.prototype.toString【文字列変換 (基数指定可)】
メモ
概要
- 文字列に変換 (実装依存)
- 文字列 ⇔ 数値 については、Number【数値】オブジェクトのメモ 参照
外部リンク
- ECMA-262 (英語)
Number.prototype.toString ( [ radix ] ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
number.toString( [radix] )
変換文字列 (基数が 10 以外は実装依存)
radix 基数 (2~36 / 省略:10)
RangeError 例外 radix (基数)が 2~36 の整数値に変換不可
例
var num = 15;
console.log(num.toString()); // 出力:15
console.log(num.toString(2)); // 出力:1111
console.log(num.toString(10)); // 出力:15
console.log(num.toString(16)); // 出力:f
console.log(num.toString(37)); // RangeError 例外 (範囲外の基数)