ale*_*lex 342
你可以用......
Math.trunc()
(截断分数部分,也见下文)Math.floor()
(向下)Math.ceil()
(围捕) Math.round()
(舍入到最接近的整数)...取决于您想要删除小数的方式.
Math.trunc()
尚未在所有平台上支持(即IE),但在此期间您可以轻松使用polyfill.
通过使用按位运算符(.eg |0
)来截断具有出色平台支持的小数部分的另一种方法.在数字上使用按位运算符的副作用是它将其操作数视为带符号的32位整数,因此删除小数部分.请记住,这也会破坏大于32位的数字.
您可能还在谈论使用浮点运算进行十进制舍入的不准确性.
Bra*_*iak 37
您还可以使用按位运算符截断小数.
例如
var x = 9 / 2;
console.log(x); // 4.5
x = ~~x;
console.log(x); // 4
x = -3.7
console.log(~~x) // -3
console.log(x | 0) // -3
console.log(x << 0) // -3
Run Code Online (Sandbox Code Playgroud)
按位运算比数学函数更有效.双不是位运算符也似乎略微跑赢大盘x | 0
,并x << 0
通过一个微不足道的位操作.
// 952 milliseconds
for (var i = 0; i < 1000000; i++) {
(i * 0.5) | 0;
}
// 1150 milliseconds
for (var i = 0; i < 1000000; i++) {
(i * 0.5) << 0;
}
// 1284 milliseconds
for (var i = 0; i < 1000000; i++) {
Math.trunc(i * 0.5);
}
// 939 milliseconds
for (var i = 0; i < 1000000; i++) {
~~(i * 0.5);
}
Run Code Online (Sandbox Code Playgroud)
另外值得注意的是,按位非运算符优先于算术运算,因此您可能需要用括号括起计算以获得预期结果:
x = -3.7
console.log(~~x * 2) // -6
console.log(x * 2 | 0) // -7
console.log(x * 2 << 0) // -7
console.log(~~(x * 2)) // -7
console.log(x * 2 | 0) // -7
console.log(x * 2 << 0) // -7
Run Code Online (Sandbox Code Playgroud)
有关double bitwise not运算符的更多信息可以在Double bitwise NOT(~~)找到
Har*_*til 28
你也可以这样做
parseInt(a/b)
Run Code Online (Sandbox Code Playgroud)
小智 23
例如:
var x = 9.656;
x.toFixed(0); // returns 10
x.toFixed(2); // returns 9.66
x.toFixed(4); // returns 9.6560
x.toFixed(6); // returns 9.656000
Run Code Online (Sandbox Code Playgroud)
或者
parseInt("10"); // returns 10
parseInt("10.33"); // returns 10
parseInt("10 20 30"); // returns 10
parseInt("10 years"); // returns 10
parseInt("years 10"); // returns NaN
Run Code Online (Sandbox Code Playgroud)
Mah*_*ian 17
你也可以使用以下代码在小数点后面显示一定数量的数字(这里是2位数):
var num = (15.46974).toFixed(2)
console.log(num) // 15.47
console.log(typeof num) // string
Run Code Online (Sandbox Code Playgroud)
小智 8
使用Math.round()
功能.
Math.round(65.98) // will return 66
Math.round(65.28) // will return 65
Run Code Online (Sandbox Code Playgroud)
以下是在之前帖子的帮助下的详细压缩解释:
1. Math.trunc() :用于删除点后面的数字。它隐式转换。但是,IE 不支持。
例子:
Math.trunc(10.5) // 10
Math.trunc(-10.5) // -10
Run Code Online (Sandbox Code Playgroud)
其他替代方法:使用按位非运算符:
例子:
x= 5.5
~~x // 5
Run Code Online (Sandbox Code Playgroud)
2. Math.floor() :用于给出可能的最小整数值。所有浏览器都支持它。
例子:
Math.floor(10.5) // 10
Math.floor(-10.5) // -11
Run Code Online (Sandbox Code Playgroud)
3. Math.ceil() :用于给出可能的最大整数值。所有浏览器都支持它。
例子:
Math.ceil(10.5) // 11
Math.ceil(-10.5) // -10
Run Code Online (Sandbox Code Playgroud)
4. Math.round() :四舍五入到最接近的整数。所有浏览器都支持它。
例子:
Math.round(10.5) // 11
Math.round(-10.5)// -10
Math.round(10.49) // 10
Math.round(-10.51) // -11
Run Code Online (Sandbox Code Playgroud)
使用ES2015,可以使用Math.trunc().
Math.trunc(2.3) // 2
Math.trunc(-2.3) // -2
Math.trunc(22222222222222222222222.3) // 2.2222222222222223e+22
Math.trunc("2.3") // 2
Math.trunc("two") // NaN
Math.trunc(NaN) // NaN
Run Code Online (Sandbox Code Playgroud)
它在IE11或更低版本中不受支持,但在Edge和其他所有现代浏览器中都可以使用.
归档时间: |
|
查看次数: |
289278 次 |
最近记录: |