移位 (>>) 运算符
移位运算符将数字向 0 舍入:
let x = 4.63; let z = x >> 0; //it is same as we are dividing the value by 1. console.log(+z);
XOR (^) 运算符
let x = 4.49; let z = x ^ 0; console.log(+z);
无符号移位 (>>>) 运算符
无符号移位将数字向 0 舍入:
let x = 5.68; let z = x >>> 0; console.log(+z);
按位 OR (|) 运算符
OR 运算符将数字向 0 舍入。
let x = 4.67; let z = x | 0; console.log(+z);
Math.trunc()
math.trunc() 函数删除任何小数位并返回数字的整数部分:
let x = 5.49; let z = Math.trunc(x); console.log(+z);
数字
JavaScript 中有两种类型的数字:Regular 和 Boirnt。
正则也称为“双精度浮点数”。
数学是一个内置对象,它具有数学常数和函数的属性和方法。
数学对象使用 Number 类型而不是 Boirnt。
Math.ceil()
Math.ceil() 函数将数字向上舍入为下一个最大的整数或者整数:
let x = 5.49; let z = Math.ceil(x); console.log("Converted value of " + x + " is " + z);
parseInt()
parseInt() 函数解析字符串参数并返回给定基数的整数。
let x = 1.54; let z = parseInt(x); console.log(+z);
双按位非 (~~) 运算符
如果操作数是一个数字并且它不是 NaN 或者 Infinity,则 ~~ 运算符会将数字向 0 舍入。
let x = 5.49; let z = ~~x; console.log(+z);
Math.floor()
math.floor 函数将作为参数传递的数字向下舍入到最接近的整数:
let x = 5.49; let z = Math.floor(x); console.log("Converted value of " + x + " is " + z);
Math.round()
math.round() 函数返回一个四舍五入到最接近的整数的数值:
let x = 5.49; let z = Math.round(x); console.log("Converted value of " + x + " is " + z);
JavaScript 中有多种简单的方法可以将浮点数转换为整数。
让我们讨论它们中的每一个并查看示例。
这里有几个用于舍入的内置函数。
日期:2020-06-02 22:16:12 来源:oir作者:oir