javascript怎么取整不要小数
时间:2022-03-28 14:50
方法:1、用“parseInt(数值)”语句取整;2、用“数值.toFixed(0)”语句取整;3、用“Math.ceil(数值)”语句取整;4、用“Math.floor(数值)”语句取整;5、用“Math.round(数值)”语句取整等。 本教程操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。 在javascript中,一个数值想不要小数(去掉小数部分),可以进行取整操作。下面给大家介绍一些javascript取整的方法。 javascript怎么取整(不要小数)的方法 1. parseInt() 2. Number.toFixed(0) 3. Math.ceil() 4. Math.floor() 5. Math.round() 6. Math.trunc() 7. 双按位非取整 8. 按位运或取整 9. 按位异或取整 【相关推荐:javascript视频教程、web前端】 以上就是javascript怎么取整不要小数的详细内容,更多请关注gxlsystem.com其它相关文章!// js内置函数,注意接受参数是string,所以调用该方法时存在类型转换
parseInt(1.5555) // => 1
// 注意toFixed返回的字符串,若想获得整数还需要做类型转换
1.5555.toFixed(0) // => "1"
// 向上取整
Math.ceil(1.5555) // => 2
// 向下取整
Math.floor(1.5555) // => 1
// 四舍五入取整
Math.round(1.5555) // => 2
Math.round(1.4999) // => 1
// 舍弃小数取整
Math.trunc(1.5555) // => 1
// 利用位运算取整,仅支持32位有符号整型数,小数位会舍弃,下同
~~1.5555 // => 1
1.5555 | 0 // => 1
1.5555^0 // => 1