Javascript 逗号后将数字四舍五入为 2 位
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4098685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rounding numbers to 2 digits after comma
提问by Sten Van den Bergh
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?
我不知道该怎么做?我正在添加逗号数字,结果当然总是一个逗号后有太多数字的数字。任何人?
回答by Andrei
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.
更新:请记住,在 2010 年最初编写答案时,波纹管函数 toFixed() 的工作方式略有不同。toFixed() 现在似乎做了一些四舍五入,但不是以严格的数学方式。所以要小心。做你的测试......正如数学家所期望的那样,下面描述的方法可以很好地进行四舍五入。
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:
toFixed()
- 方法将数字转换为字符串,保留指定的小数位数。它实际上并没有对数字进行四舍五入,而是截断了数字。Math.round(n)
- 将数字四舍五入到最接近的整数。于是转向:
0.5 -> 1; 0.05 -> 0
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,只保留小数点后第二位;您可以执行以下操作(这是分步概念):
0.55555 * 100
= 55.555Math.Round(55.555)
-> 56.00056.000 / 100
= 0.56000(0.56000).toFixed(2)
-> 0.56
0.55555 * 100
= 55.555Math.Round(55.555)
-> 56.00056.000 / 100
= 0.56000(0.56000).toFixed(2)
-> 0.56
and this is the code:
这是代码:
(Math.round(number * 100)/100).toFixed(2);
回答by Jacob Relkin
EDIT 2:
编辑 2:
Use the Number
object's toFixed
method like this:
像这样使用Number
对象的toFixed
方法:
var num = Number(0.005) // The Number() only visualizes the type and is not needed
var roundedString = num.toFixed(2);
var rounded = Number(roudedString); // toFixed() returns a string (often suitable for printing already)
It rounds 42.0054321 to 42.01
它将 42.0054321 舍入到 42.01
It rounds 0.005 to 0.01
它舍入 0.005 到 0.01
It rounds -0.005 to -0.01 (So the absolute value increases on rounding at .5 border)
它将 -0.005 舍入到 -0.01(因此绝对值在 0.5 边界处舍入时增加)
回答by Raivo
This worked for me:
这对我有用:
var new_number = float.toFixed(2);
Example:
例子:
var my_float = 0.6666
my_float.toFixed(3) # => 0.667
回答by Romain Vergnory
Previous answers forgot to type the output as an Number again. There is several ways to do this, depending on your tastes.
以前的答案忘记再次将输出输入为数字。有几种方法可以做到这一点,这取决于您的口味。
+my_float.toFixed(2)
Number(my_float.toFixed(2))
parseFloat(my_float.toFixed(2))
回答by Tu4n3r
This is not really CPU friendly, but :
这不是真正的 CPU 友好,但是:
Math.round(number*100)/100
works as expected.
按预期工作。
回答by Just Shadow
Though we have many answers here with plenty of useful suggestions, each of them still missessome steps.
So here is a completesolution wrapped into small function:
虽然我们在这里有很多答案和很多有用的建议,但他们每个人仍然遗漏了一些步骤。
所以这里有一个完整的解决方案,包含在小函数中:
function roundToTwoDigitsAfterComma(floatNumber) {
return parseFloat((Math.round(floatNumber * 100) / 100).toFixed(2));
}
Just in case you are interested how this works:
以防万一您对它的工作原理感兴趣:
- Multiple with 100 and then do round to keep precision of 2 digits after comma
- Divide back into 100 and use
toFixed(2)
to keep 2 digits after comma and throw other unuseful part - Convert it back to float by using
parseFloat()
function astoFixed(2)
returns string instead
- 乘以 100,然后进行舍入以保持逗号后 2 位的精度
- 再除以100并用于
toFixed(2)
保留逗号后的2位数字并丢弃其他无用部分 - 使用
parseFloat()
函数作为toFixed(2)
返回字符串将其转换回浮点数
Note:If you keep last 2 digits after comma because of working with monetary values, and doing financial calculationskeep in mind that it's not a good ideaand you'd better use integer values instead.
注意:如果因为使用货币值而保留逗号后的最后 2 位数字,并且进行财务计算,请记住这不是一个好主意,最好使用整数值。
回答by Phoenix
use the below code.
使用下面的代码。
alert(+(Math.round(number + "e+2") + "e-2"));
回答by Gehtdich Nichtsan
I use this:
我用这个:
function round(value, precision) {
if(precision == 0)
return Math.round(value);
exp = 1;
for(i=0;i<precision;i++)
exp *= 10;
return Math.round(value*exp)/exp;
}