我试图在Javascript中添加两个数字:
var output;
output = parseInt(a)+parseInt(b);
alert(output);
Run Code Online (Sandbox Code Playgroud)
它给出了错误的output
价值,例如if a = 015
和b = 05
.为什么会这样?上述例子的预期结果应为20.
如果您在数字前加上0
,则在基数8中指定它们,015
因此为13,总和为18.
使用第二个parseInt
参数强制基数:
var a = '015', b = '05';
var output;
output = parseInt(a, 10) + parseInt(b, 10);
alert(output); // alerts 20
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
238 次 |
最近记录: |