将逗号后的数字四舍五入为 2 位数字
我不知道该怎么做?我正在添加逗号数字,结果当然总是一个逗号后数字太多的数字。有人吗?
I have no idea how to do this? I'm adding comma numbers, result is of course always a number with way too many digits after the comma. anyone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
编辑 2:
使用
Number
对象的toFixed
方法,如下所示:将 42.0054321 舍入到 42.01
将 0.005 舍入到 0.01
将 -0.005 舍入到 -0.01 (因此绝对值在 0.5 边界舍入时增加)
jsFiddle 示例
EDIT 2:
Use the
Number
object'stoFixed
method like this:It rounds 42.0054321 to 42.01
It rounds 0.005 to 0.01
It rounds -0.005 to -0.01 (So the absolute value increases on rounding at .5 border)
jsFiddle example
更新:请记住,在 2010 年最初编写答案时,下面的函数 toFixed() 的工作方式略有不同。 toFixed() 现在似乎做了一些舍入,但不是以严格的数学方式。所以要小心。做你的测试...下面描述的方法将很好地进行舍入,正如数学家所期望的那样。
toFixed()
- 方法将数字转换为字符串,并保留指定的小数位数。它实际上并不对数字进行四舍五入,而是截断数字。Math.round(n)
- 将数字四舍五入到最接近的整数。由此转:0.5->0.5 1;
0.05-> 0
所以如果你想四舍五入,比如说数字 0.55555,只到小数点后第二位;您可以执行以下操作(这是逐步概念):
0.55555 * 100
= 55.555Math.Round(55.555)
-> 56.00056.000 / 100
= 0.56000(0.56000).toFixed(2)
-> 0.56这是代码:
UPDATE: Keep in mind, at the time the answer was initially written in 2010, the bellow function toFixed() worked slightly different. toFixed() seems to do some rounding now, but not in the strictly mathematical manner. So be careful with it. Do your tests... The method described bellow will do rounding well, as mathematician would expect.
toFixed()
- method converts a number into a string, keeping a specified number of decimals. It does not actually rounds up a number, it truncates the number.Math.round(n)
- rounds a number to the nearest integer. Thus turning:0.5 -> 1;
0.05 -> 0
so if you want to round, say number 0.55555, only to the second decimal place; you can do the following(this is step-by-step concept):
0.55555 * 100
= 55.555Math.Round(55.555)
-> 56.00056.000 / 100
= 0.56000(0.56000).toFixed(2)
-> 0.56and this is the code:
这对我有用:
示例:
This worked for me:
Example:
以前的答案忘记再次将输出输入为数字。有多种方法可以做到这一点,具体取决于您的口味。
Previous answers forgot to type the output as an Number again. There is several ways to do this, depending on your tastes.
这并不是真正的 CPU 友好,但是:
按预期工作。
This is not really CPU friendly, but :
works as expected.
尽管我们在这里有很多答案和很多有用的建议,但每个答案仍然遗漏一些步骤。
因此,这里有一个封装在小函数中的完整解决方案:
以防万一您对其工作原理感兴趣:
逗号后
toFixed(2)
保留后面的 2 位数字逗号并抛出其他无用部分
parseFloat()
函数将其转换回浮点型toFixed(2)
返回字符串注意:如果您因使用货币值而保留逗号后的最后 2 位数字,并且进行财务计算,请保留请记住,这不是一个好主意,而您最好使用整数值。
Though we have many answers here with plenty of useful suggestions, each of them still misses some steps.
So here is a complete solution wrapped into small function:
Just in case you are interested how this works:
after comma
toFixed(2)
to keep 2 digits aftercomma and throw other unuseful part
parseFloat()
function astoFixed(2)
returns string insteadNote: If you keep last 2 digits after comma because of working with monetary values, and doing financial calculations keep in mind that it's not a good idea and you'd better use integer values instead.
使用下面的代码。
use the below code.
我用这个:
I use this:
上述所有建议似乎并不适用于所有数字(包括负数)
0.075 => 0.08
-0.075 => -0.08
0.005 => 0.01
-0.005 => -0.01
Math.sign(num) * (Math.round((Math.abs(num) + Number.EPSILON) * 1e2) / 1e2);
All the suggestions above do not seem to be working with all numbers (negative ones included)
0.075 => 0.08
-0.075 => -0.08
0.005 => 0.01
-0.005 => -0.01
Math.sign(num) * (Math.round((Math.abs(num) + Number.EPSILON) * 1e2) / 1e2);
我使用 tofixd() 仍然工作正常
I'am use tofixd() still working ok