この文書は翻訳中です。他国語のままの部分などがあるのはその為です。
是非お気軽に MDN に登録して翻訳に参加し、私たちの手助けをして下さい!
概要
コア関数
文字列の引数をパースし、指定された基数の整数を返します。
構文
var intValue = parseInt(string[, radix]);
引数
-
string - パースしたい値を表す文字列。
-
radix - 上述の string の基数を表す整数。
説明
parseInt はトップレベルの関数であり、何のオブジェクトとも関連付けられていません。
parseInt 関数はその一番目の引数である文字列をパースし、指定された基数の整数を返します。例えば、10 を基数にすると、10 進表記に、8 だと、8 進表記に、16 だと、16 進表記に…という風になります。 10 以上の基数だと、9 より大きい数を表すのに、アルファベットの文字が使われます。例えば、16 進表記(基数は 16)だと、A から F が使われます。
parseInt が、指定された基数においては、数ではない文字に出会った場合、その文字とそれに続く文字の全てを無視し、その地点までパースされた値の整数を返します。 parseInt は、整数の値まで数を切り捨てます。 文字列の前後に空白があっても問題ありません。
基数が指定されなかったり、0 が指定された場合、JavaScript は以下のように解釈します。:
- 入力
stringが "0x" で始まっている場合、基数は 16(16 進表記)になります。
- 入力
stringが "0" で始まっている場合、基数は 8 (8 進表記)になります。この機能は、非推奨です。
- 入力
stringが他の値で始まっている場合、基数は、10(10進表記)になります。
一番目の文字が数に変換できない場合、parseInt は、NaN を返します。 算術の用途では、NaN の値は、どんな基数でも数ではありません。parseInt の結果が NaN であるかどうかを判定することは、isNaN を呼び出すことで可能です。NaN が 算術演算子に演算対象として渡された場合、演算結果もNaN になるでしょう。
数を特定の基数の文字列リテラルに変換するためには、intValue.toString(radix) を使用してください。
例
Example: Using parseInt
次の例の各行のコードは、全て 15 を返します。
parseInt("0xF", 16);
parseInt("F", 16);
parseInt("17", 8);
parseInt(021, 8);
parseInt("015", 10);
parseInt(15.99, 10);
parseInt("FXX123", 16);
parseInt("1111", 2);
parseInt("15*3", 10);
parseInt("15e2", 10);
parseInt("15px", 10);
parseInt("12", 13);
次の例の各行のコードは、全て NaN を返します。
parseInt("Hello", 8); // Not a number at all
parseInt("546", 2); // Digits are not valid for binary representations
次の例の各行のコードは、全て -15: を返します。
parseInt("-F", 16);
parseInt("-0F", 16);
parseInt("-0XF", 16);
parseInt(-10, 16);
parseInt(-15.1, 10)
parseInt(" -17", 8);
parseInt(" -15", 10);
parseInt("-1111", 2);
parseInt("-15e1", 10);
parseInt("-12", 13);
次の例は 224 を返します。
parseInt("0e0", 16);
Octal Interpretations with No Radix
Although discouraged by ECMAScript 3, many implementations interpret a numeric string beginning with a leading 0 as octal. The following may have an octal result.
困った事に、ECMAScript 3 では、parseInt の引数とする数字の先頭の文字が 0 で有った場合の解釈を実装側に委ねていました。その為、多くの実装で先頭が 0 から始まる数値文字列が 8 進数として解釈され、しかしながら必ず 8 進数になるとも限りません。概ね、以下の様な結果になるでしょう。
parseInt("0e0"); // 0
parseInt("08"); // 0, '8' is not an octal digit.
ECMAScript 5 で 8 進数の解釈を許す表現が仕様から削除された
The ECMAScript 5 specification of the function parseInt no longer allows implementations to treat Strings beginning with a 0 character as octal values. ECMAScript 5 states:
The parseInt function produces an integer value dictated by interpretation of the contents of the string argument according to the specified radix. Leading white space in string is ignored. If radix is undefined or 0, it is assumed to be 10 except when the number begins with the character pairs 0x or 0X, in which case a radix of 16 is assumed. If radix is 16, number may also optionally begin with the character pairs 0x or 0X.
This differs from ECMAScript 3, which discouraged but allowed octal interpretation.
Since many implementations have not adopted this behavior as of 2011, and because older browsers must be supported, always specify a radix.
Mozilla Developer Network