如何在 Javascript 中对数字进行四舍五入?
如何在 JavaScript 中对数字进行舍入?
math.round()
不起作用,因为它会将其四舍五入到最接近的小数。
我不确定除了在保留第一位的小数点处将其分开之外是否还有更好的方法。一定有...
How can I round down a number in JavaScript?
math.round()
doesn't work because it rounds it to the nearest decimal.
I'm not sure if there is a better way of doing it other than breaking it apart at the decimal point at keeping the first bit. There must be...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
使用 Math.floor() 是实现此目的的一种方法。
更多信息:https://developer.mozilla。 org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor
Using
Math.floor()
is one way of doing this.More information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor
向负无穷大舍入 -
Math.floor()
可以使用
Math.trunc()
。较旧的浏览器不支持此功能。如果您需要支持这些,可以使用Math.ceil()
表示负数,使用Math.floor()
表示正数。Round towards negative infinity -
Math.floor()
Round towards zero can be done using
Math.trunc()
. Older browsers do not support this function. If you need to support these, you can useMath.ceil()
for negative numbers andMath.floor()
for positive numbers.Math.floor()
可以工作,但与使用按位OR
运算相比,它非常慢:编辑
Math.floor()
不比使用 | 慢操作员。感谢 Jason S 检查我的工作。这是我用来测试的代码:
Math.floor()
will work, but it's very slow compared to using a bitwiseOR
operation:EDIT
Math.floor()
is not slower than using the | operator. Thanks to Jason S for checking my work.Here's the code I used to test:
如果需要向下舍入到特定的小数位数,可以尝试使用此函数
示例
You can try to use this function if you need to round down to a specific number of decimal places
examples
将
数字
四舍五入到0
(又名“截断其小数部分”)可以通过减去其有符号小数部分number % 1来完成
:像
Math.floor
(向-Infinity
舍入)一样,这种方法非常准确。不过,
-0
、+Infinity
和-Infinity
的处理存在差异:Rounding a
number
towards0
(aka "truncating its fractional part") can be done by subtracting its signed fractional partnumber % 1
:Like
Math.floor
(rounds towards-Infinity
) this method is perfectly accurate.There are differences in the handling of
-0
,+Infinity
and-Infinity
though:要向下舍入到负无穷大,请使用:
要向下舍入到零(如果数字可以舍入到 -2147483648 和 2147483647 之间的 32 位整数),请使用:
要向下舍入到零(对于任何数字),请使用:
To round down towards negative infinity, use:
To round down towards zero (if the number can round to a 32-bit integer between -2147483648 and 2147483647), use:
To round down towards zero (for any number), use:
今天摆弄别人的代码,发现以下内容似乎也向下舍入:
有关符号传播右移(>>)的更多信息,请参阅 MDN 位运算符
我花了一段时间才弄清楚它在做什么:D
但正如上面强调的那样,Math.floor() 可以工作并且我认为看起来更具可读性。
Was fiddling round with someone elses code today and found the following which seems rounds down as well:
For more info on the Sign-propagating right shift(>>) see MDN Bitwise Operators
It took me a while to work out what this was doing :D
But as highlighted above, Math.floor() works and looks more readable in my opinion.
这是我发现工作可靠的最佳解决方案。
致谢:Jack L Moore 的博客
This was the best solution I found that works reliably.
Credit to: Jack L Moore's blog
您需要将 -1 向下舍入一半,然后乘以 -1,如下例所示。
You need to put -1 to round half down and after that multiply by -1 like the example down bellow.
这是一个简单示例中使用的 math.floor。这可能有助于新开发人员了解如何在函数中使用它以及它的作用。希望有帮助!
Here is math.floor being used in a simple example. This might help a new developer to get an idea how to use it in a function and what it does. Hope it helps!