将逗号后的数字四舍五入为 2 位数字

发布于 2024-09-30 12:00:13 字数 49 浏览 6 评论 0原文

我不知道该怎么做?我正在添加逗号数字,结果当然总是一个逗号后数字太多的数字。有人吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

请别遗忘我 2024-10-07 12:00:13

编辑 2

使用 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(roundedString); // toFixed() returns a string (often suitable for printing already)

将 42.0054321 舍入到 42.01

将 0.005 舍入到 0.01

将 -0.005 舍入到 -0.01 (因此绝对值在 0.5 边界舍入时增加)

jsFiddle 示例

EDIT 2:

Use the Number object's toFixed method like this:

var num = Number(0.005) // The Number() only visualizes the type and is not needed
var roundedString = num.toFixed(2);
var rounded = Number(roundedString); // toFixed() returns a string (often suitable for printing already)

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

晒暮凉 2024-10-07 12:00:13

更新:请记住,在 2010 年最初编写答案时,下面的函数 toFixed() 的工作方式略有不同。 toFixed() 现在似乎做了一些舍入,但不是以严格的数学方式。所以要小心。做你的测试...下面描述的方法将很好地进行舍入,正如数学家所期望的那样。

  • toFixed() - 方法将数字转换为字符串,并保留指定的小数位数。它实际上并不对数字进行四舍五入,而是截断数字。
  • Math.round(n) - 将数字四舍五入到最接近的整数。由此转:

0.5->0.5 1;
0.05-> 0

所以如果你想四舍五入,比如说数字 0.55555,只到小数点后第二位;您可以执行以下操作(这是逐步概念):

  • 0.55555 * 100 = 55.555
  • Math.Round(55.555) -> 56.000
  • 56.000 / 100 = 0.56000
  • (0.56000).toFixed(2) -> 0.56

这是代码:

(Math.round(number * 100)/100).toFixed(2);

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.555
  • Math.Round(55.555) -> 56.000
  • 56.000 / 100 = 0.56000
  • (0.56000).toFixed(2) -> 0.56

and this is the code:

(Math.round(number * 100)/100).toFixed(2);
苏别ゝ 2024-10-07 12:00:13

这对我有用:

var new_number = float.toFixed(2);

示例:

var my_float = 0.6666

my_float.toFixed(3) # => 0.667

This worked for me:

var new_number = float.toFixed(2);

Example:

var my_float = 0.6666

my_float.toFixed(3) # => 0.667
吐个泡泡 2024-10-07 12:00:13

以前的答案忘记再次将输出输入为数字。有多种方法可以做到这一点,具体取决于您的口味。

+my_float.toFixed(2)

Number(my_float.toFixed(2))

parseFloat(my_float.toFixed(2))

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))
清音悠歌 2024-10-07 12:00:13

这并不是真正的 CPU 友好,但是:

Math.round(number*100)/100

按预期工作。

This is not really CPU friendly, but :

Math.round(number*100)/100

works as expected.

心凉怎暖 2024-10-07 12:00:13

尽管我们在这里有很多答案和很多有用的建议,但每个答案仍然遗漏一些步骤。
因此,这里有一个封装在小函数中的完整解决方案:

function roundToTwoDigitsAfterComma(floatNumber) {
    return parseFloat((Math.round(floatNumber * 100) / 100).toFixed(2));
}

以防万一您对其工作原理感兴趣:

  1. 乘以 100,然后进行舍入以保持 2 位精度
    逗号后
  2. 除回 100 并使用 toFixed(2) 保留后面的 2 位数字
    逗号并抛出其他无用部分
  3. 使用 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:

function roundToTwoDigitsAfterComma(floatNumber) {
    return parseFloat((Math.round(floatNumber * 100) / 100).toFixed(2));
}

Just in case you are interested how this works:

  1. Multiple with 100 and then do round to keep precision of 2 digits
    after comma
  2. Divide back into 100 and use toFixed(2) to keep 2 digits after
    comma and throw other unuseful part
  3. Convert it back to float by using parseFloat() function as
    toFixed(2) returns string instead

Note: 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.

Hello爱情风 2024-10-07 12:00:13

使用下面的代码。

alert(+(Math.round(number + "e+2")  + "e-2"));

use the below code.

alert(+(Math.round(number + "e+2")  + "e-2"));
森末i 2024-10-07 12:00:13

我用这个:

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;
}

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;
}

夜访吸血鬼 2024-10-07 12:00:13

上述所有建议似乎并不适用于所有数字(包括负数)

  1. 0.075 => 0.08

  2. -0.075 => -0.08

  3. 0.005 => 0.01

  4. -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)

  1. 0.075 => 0.08

  2. -0.075 => -0.08

  3. 0.005 => 0.01

  4. -0.005 => -0.01

    Math.sign(num) * (Math.round((Math.abs(num) + Number.EPSILON) * 1e2) / 1e2);

路弥 2024-10-07 12:00:13
console.log((Math.random().toFixed(2)));

我使用 tofixd() 仍然工作正常

console.log((Math.random().toFixed(2)));

I'am use tofixd() still working ok

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文