Rounding numbers to 2 digits after comma

JavascriptMathRounding

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

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

Solution 3 - Javascript

This worked for me:

var new_number = float.toFixed(2);

Example:

var my_float = 0.6666

my_float.toFixed(3) # => 0.667

Solution 4 - Javascript

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

Solution 5 - Javascript

This is not really CPU friendly, but :

Math.round(number*100)/100

works as expected.

Solution 6 - Javascript

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.

Solution 7 - Javascript

use the below code.

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

Solution 8 - Javascript

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

Solution 9 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSten Van den BerghView Question on Stackoverflow
Solution 1 - JavascriptJacob RelkinView Answer on Stackoverflow
Solution 2 - JavascriptAndreiView Answer on Stackoverflow
Solution 3 - JavascriptRaivoView Answer on Stackoverflow
Solution 4 - JavascriptRomain VergnoryView Answer on Stackoverflow
Solution 5 - JavascriptTu4n3rView Answer on Stackoverflow
Solution 6 - JavascriptJust ShadowView Answer on Stackoverflow
Solution 7 - JavascriptPhoenixView Answer on Stackoverflow
Solution 8 - JavascriptGehtdich NichtsanView Answer on Stackoverflow
Solution 9 - JavascriptSGOmSView Answer on Stackoverflow