「JavaScript入門」parseInt()の使い方

2021年8月4日

書式
parseInt(string [, radix])
string: この引数が文字列でない場合、抽象操作 ToString を用いて文字列に変換されます。
radix 省略可 2 ~ 36 までの整数で、string の基数 (数学的記数法の底) を表します。
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function funcA(x, base) {
const res = parseInt(x, base);
if (isNaN(res)) { return 0; }
return res * 100;
}
console.log(funcA(' 0xF', 16));
console.log(funcA('121', 2));
function funcA(x, base) { const res = parseInt(x, base); if (isNaN(res)) { return 0; } return res * 100; } console.log(funcA(' 0xF', 16)); console.log(funcA('121', 2));
function funcA(x, base) {
  const res = parseInt(x, base);
  if (isNaN(res)) { return 0; }
  return res * 100;
}

console.log(funcA(' 0xF', 16));

console.log(funcA('121', 2));

結果
1500
100

JavaScript

Posted by arkgame