Skip to content Skip to sidebar Skip to footer

How Can I Round A Float Such That There Are Only Two Trailing Digits After The Decimal Point?

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.5

Solution 1:

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

Solution 2:

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

varvalue = 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.

Solution 3:

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/

Solution 4:

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)

functiontoStringWithSignificantDigits( x, len ){
    if(x==0) return x.toFixed(len-1); // makes little sense for 0var 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));  
}


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

testIt(12.345,6);  // 12.3450testIt(12.345,5);  // 12.345testIt(12.345,4);  // 12.35testIt(12.345,3);  // 12.3testIt(12.345,2);  // 12testIt(12.345,1);  // 10testIt(0.012345,7);  // 0.01234500testIt(0.012345,6);  // 0.0123450testIt(0.012345,5);  // 0.012345testIt(0.012345,4);  // 0.01235testIt(0.012345,3);  // 0.0123testIt(0.012345,2);  // 0.012testIt(0.012345,1);  // 0.01testIt(0,3); // 0.00, zero is a special case, as it has no significant digit

Post a Comment for "How Can I Round A Float Such That There Are Only Two Trailing Digits After The Decimal Point?"