How can I round a float such that there are only two trailing digits after the decimal point?

5.6k views Asked by At

I have a float number like 137.57667565656 but I would like to round it such that there are only two trailing digits after the decimal point like the new float number will be 137.58.

I tried this so far:

(Math.round((value*100)/100)).toFixed(2).toString();

But unfortunately, it rounds my value to 137.00. It adds the decimals places as zeroes, why?

How can I achieve the above?

4

There are 4 answers

4
RobG On BEST ANSWER

What did you expect?

(value*100)/100

simply returns the original value of value, so

Math.round((value*100)/100))

is identical to:

Math.round(value)

you then have:

Math.round(value).toFixed(2).toString();

so value is rounded to an integer, toFixed will add two decimal places and return a string so the toString part is redundant. If you wish to round value to four decimal places, then:

value.toFixed(4)

will do the job:

var x = 137.57667565656;

console.log(x.toFixed(4)); // 137.5767

If you want to round it to 2 places but present it as 4, then:

Number(x.toFixed(2)).toFixed(4)  // 137.5800
2
Kevin Kopf On

Two zeroes are added because prior to all you do Math.round:

If the fractional portion of number is .5 or greater, the argument is rounded to the next higher integer. If the fractional portion of number is less than .5, the argument is rounded to the next lower integer.

(taken from here)

What you need is to drop Math.round:

(value*100)/100).toFixed(4).toString();

See it on JSFiddle: http://jsfiddle.net/0fe5mm95/

0
Merchako On

I have a simpler answer that works for all significant figures, not just four. JavaScript provides a built-in Number.toPrecision() function that returns a string representing a number in significant digits.

var value = 137.57667565656;
var strVal = value.toPrecision(4);
>> 137.6

There have been a lot of misconceptions about significant figures on this post. See Wikipedia to refresh your memory. See @RobG's answer for why your original method was incorrect.

0
kritzikratzi On

Edit Apparently i misunderstood the question. When I tried to answer it was (at least in the title) about significant digits.

This answer lines up with Wikipedia's definition of significant digits. It solves the general problem, for any number of significant digits, in three steps:

  1. figure out number of digits with log10 (negative if -1<x<1, e.g 5 for 12345, or -2 for 0.01)
  2. round the number at the correct position (e.g. if x=12345 with three significant digits, then round the 45 to 00)
  3. append the required number of zeros on the right

And that's all. Except the zero, which has no significant digits. (I went for "0.00" when asking for three significant digits, but 0 or 0.0 would be fine also)

function toStringWithSignificantDigits( x, len ){
    if(x==0) return x.toFixed(len-1); // makes little sense for 0
    var numDigits = Math.ceil(Math.log10(Math.abs(x)));
    var rounded = Math.round(x*Math.pow(10,len-numDigits))*Math.pow(10,numDigits-len); 
    return rounded.toFixed(Math.max(len-numDigits,0));  
}


function testIt(x,len){
    console.log( "format " + x + " to " + len + " significant digits: " + toStringWithSignificantDigits(x,len)); 
}

testIt(12.345,6);  // 12.3450
testIt(12.345,5);  // 12.345
testIt(12.345,4);  // 12.35
testIt(12.345,3);  // 12.3
testIt(12.345,2);  // 12
testIt(12.345,1);  // 10

testIt(0.012345,7);  // 0.01234500
testIt(0.012345,6);  // 0.0123450
testIt(0.012345,5);  // 0.012345
testIt(0.012345,4);  // 0.01235
testIt(0.012345,3);  // 0.0123
testIt(0.012345,2);  // 0.012
testIt(0.012345,1);  // 0.01

testIt(0,3); // 0.00, zero is a special case, as it has no significant digit