js 小数取整

小数取整

var num = 123.456;

// 常规方法
console.log(parseInt(num)); // 123

// 双重按位非
console.log(~~num); // 123

// 按位或
console.log(num | 0); // 123

// 按位异或
console.log(num ^ 0); // 123

// 左移操作符
console.log(num << 0); // 123

原文地址:https://www.cnblogs.com/taadis/p/12209169.html

时间: 2024-08-07 04:05:36

js 小数取整的相关文章

js 小数取整的函数

1.丢弃小数部分,保留整数部分 js:parseInt(7/2) 2.向上取整,有小数就整数部分加1 js: Math.ceil(7/2) 3,四舍五入. js: Math.round(7/2) 4,向下取整 js: Math.floor(7/2)

js小数取整 小数保留两位

<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <div id="aa">12.00</div> <div id="bb">12.666</div> </body></h

js中对小数取整

js中对小数取整的函数,需要的朋友可以参考下. 1.丢弃小数部分,保留整数部分 js:parseInt(7/2) 2.向上取整,有小数就整数部分加1 js: Math.ceil(7/2) 3,四舍五入. js: Math.round(7/2) 4,向下取整 js: Math.floor(7/2)

js中Math.round、parseInt、Math.floor和Math.ceil小数取整总结(转)

js中Math.round.parseInt.Math.floor和Math.ceil小数取整总结 Math.round.parseInt.Math.floor和Math.ceil 都可以返回一个整数,具体的区别请看下面的总结. 一.Math.round 作用:四舍五入,返回参数+0.5后,向下取整. 如: Math.round(5.57) //返回6 Math.round(2.4) //返回2 Math.round(-1.5) //返回-1 Math.round(-5.8) //返回-6 二.

简单的方式实现javascript 小数取整

JS: function truncateNumber(n){ return n|0; } 测试: console.log(truncateNumber(12.345)); 浏览器打印出12 简单的方式实现javascript 小数取整

js 向上取整、向下取整、四舍五入

js 向上取整.向下取整.四舍五入 CreateTime--2018年4月14日11:31:21 Author:Marydon // 1.只保留整数部分(丢弃小数部分) parseInt(5.1234);// 5 // 2.向下取整(<= 该数值的最大整数)和parseInt()一样 Math.floor(5.1234);// 5 // 3.向上取整(有小数,整数就+1) Math.ceil(5.1234); // 4.四舍五入(小数部分) Math.round(5.1234);// 5 Mat

java小数取整

java中提供三种小数取整方式 Math.ceil() Math.floor() Math.round() ceil:天花板,向上quzheng Math.ceil(11.5) = 12 Math.ceil(-11.5) = -11 floor:地,向下取整 Math.floor(11.5) = 11 Math.floor(-11.5) = -12 round:4舍5入 Math.round(x + 0.5),再向下取整 当 x = 11.5 时,Math.round(x + 0.5) = Ma

JS中对小数取整的函数

1.丢弃小数部分,保留整数部分 js:parseInt(7/2) 2.向上取整,有小数就整数部分加1 js: Math.ceil(7/2) 3,四舍五入. js: Math.round(7/2) 4,向下取整 js: Math.floor(7/2) MATH 对象的方法 FF: Firefox, N: Netscape, IE: Internet Explorer 方法 描述 FF N IE abs(x) 返回数的绝对值 1 2 3 acos(x) 返回数的反余弦值 1 2 3 asin(x)

js 中小数取整的函数

1.丢弃小数部分,保留整数部分 js:parseInt(7/2) 2.向上取整,有小数就整数部分加1 js: Math.ceil(7/2) 3,四舍五入. js: Math.round(7/2) 4,向下取整 js: Math.floor(7/2)