如何 JavaScript 向下舍入数字 - 继续
这与我之前的问题有点不同
How to round up using javascript
我有这组数字并希望四舍五入到小数点后一位。由于标准的javascript舍入功能无法满足我的要求,希望有人可以帮助我:)
以下是舍入时所需的输出:
192.168 => 192.10
192.11=> 192.10
192.21 =>192.20
192.26=> 192.20
192.30=> 192.30
This is a bit different from my question before
How to round up using javascript
I have this set of number and want to round down to 1 decimal point. Since standard javascript round up function cant meet my requirement, hope someone can help me :)
Below is the ouput needed when round down:
192.168 => 192.10
192.11 => 192.10
192.21 =>192.20
192.26 => 192.20
192.30 = > 192.30
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这不是四舍五入,
192.168 四舍五入到小数点后两位是 192.20。
但是根据您的要求:
您可能只想将其乘以 10 的倍数,取 'floor' ,然后除以相同的数字。
在这里查看:http://jsfiddle.net/7qeGH/
this is not rounding,
192.168 rounded to two decimal places is 192.20.
However for your requirement :
You might just want to multiply it by multiples of 10, take 'floor' , and then divide by same number.
see it here : http://jsfiddle.net/7qeGH/
Math.round(number * 10) / 10
怎么样?您将得到小数点后 1 位的结果,因此您可以在末尾添加零(Math.round(number * 10) / 10) + "0"
但实际上您是向下舍入而不是向上舍入。所以它会是Math.floor()。
How about
Math.round(number * 10) / 10
? You'll get a result with 1 decimal so you can add a zero at the end(Math.round(number * 10) / 10) + "0"
But actually you round down not up. So it would be
Math.floor()
.你可以尝试这样的事情:
You can try something like this :
很简单
Very simple
你不就是想打地板吗?即“向下舍入”。
Aren't you simply trying to floor? That is, "round down".