Nak*_*kib 88 javascript division integer-division
Javascript中是否有任何函数可以让你进行整数除法,我的意思是在int中得到除法答案,而不是浮点数.
var x = 455/10;
// Now x is 45.5
// Expected x to be 45
Run Code Online (Sandbox Code Playgroud)
但我希望x为45.我试图消除数字中的最后一位数字.
Nee*_*raj 210
var answer = Math.floor(x)
Run Code Online (Sandbox Code Playgroud)
我真诚地希望这会在搜索这个常见问题时帮助未来的搜索者.
ST3*_*ST3 25
var x = parseInt(455/10);
Run Code Online (Sandbox Code Playgroud)
parseInt()函数解析字符串并返回一个整数.
radix参数用于指定要使用的数字系统,例如,16(十六进制)的基数表示字符串中的数字应从十六进制数解析为十进制数.
如果省略radix参数,则JavaScript假定以下内容:
Run Code Online (Sandbox Code Playgroud)If the string begins with "0x", the radix is 16 (hexadecimal) If the string begins with "0", the radix is 8 (octal). This feature is deprecated If the string begins with any other value, the radix is 10 (decimal)