32

With .toFixed(2) I always get 2 decimals, even if the number is 2.00

Can I get "2" instead?

Example:

  • 2.00 => 2
  • 2.05 => 2.05
  • 2.053435 => 2.05
  • 2.057435 => 2.06
3

3 Answers 3

59

function toFixedIfNecessary( value, dp ){
  return +parseFloat(value).toFixed( dp );
}

console.log( toFixedIfNecessary( 1.999, 2 ));    // 2
console.log( toFixedIfNecessary( 2, 2 ));        // 2
console.log( toFixedIfNecessary( 2.1, 2 ));      // 2.1
console.log( toFixedIfNecessary( 2.05, 2 ));     // 2.05
console.log( toFixedIfNecessary( 2.05342, 2 ));  // 2.05
console.log( toFixedIfNecessary( 2.04999, 2 ));  // 2.05
console.log( toFixedIfNecessary( 2.04499, 2 ));  // 2.04
console.log( toFixedIfNecessary( 2.053435, 2 )); // 2.05
console.log( toFixedIfNecessary( 2.057435, 2 )); // 2.06

9
  • 5
    What sworcery is this? Can someone explain why +parseFloat has this behaviour?
    – homebrand
    Commented Dec 25, 2017 at 8:33
  • 4
    @homebrand the + converts the string output of toFixed back to a float. Try +"2.00" in the console
    – Rani
    Commented May 11, 2019 at 15:47
  • 1
    This use case scenario is not covered: console.log( toFixedIfNecessary(0.000000032, 10 )); // 3.2e-8 Commented Aug 18, 2021 at 5:49
  • 1
    @wintercounter Of course, 66.666 rounds to 66.67 to 2 decimal places. If you were expecting 66.66 then that is truncating the number and not rounding and is an entirely different question.
    – MT0
    Commented Feb 10, 2023 at 9:22
  • 1
    @GeraldoB.Landre That is a display/formatting problem and not a rounding problem. toFixedIfNecessary(value, 10 ) returns a number with at most 10 decimal places and correctly rounded; if your number is small then JavaScript's default method of displaying it is to use scientific notation and your issue is that you want to format the number differently not that it is rounded incorrectly. For formatting, look at some of the later answers to this question such as this answer (and my comment on it).
    – MT0
    Commented Feb 11, 2023 at 11:09
3

You can use Math.round():

var number = 2.005;
var number2 = 2.558934;
var number3 = 1.005;

function round(value, decimals) {
    return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}

console.log(round(number, 2)) // > 2.01
console.log(round(number2, 2)) // > 2.56
console.log(round(number3, 2)) // > 1.01

4
  • Math.round( 1.005 * 100 ) / 100 returns 1
    – MT0
    Commented Aug 26, 2015 at 14:49
  • 1
    And it should return 1, because we want maximum 2 decimal places, but if you input 1.05 it will return 1.05, or if we input 1.051264 it will return 1.05 Commented Aug 26, 2015 at 14:56
  • 3
    @NevenIgnjic 1.005 to 2dp is 1.01 not 1
    – MT0
    Commented Aug 26, 2015 at 14:58
  • It depends, I came across two types of rounding when the input is at the half between to values it needs to snap. 1. Snap to higher or lower value 2. Snap to closest even or odd number So it all depends what kind of software is calculating this, but to the OP these things don't matter. Commented Aug 26, 2015 at 15:01
2
function toFixedIfNecessary(value, dp){
  return +value.toFixed(dp);
}

console.log( toFixedIfNecessary( 1.999, 2 ));    // 2
console.log( toFixedIfNecessary( 2, 2 ));        // 2
console.log( toFixedIfNecessary( 2.1, 2 ));      // 2.1
console.log( toFixedIfNecessary( 2.05, 2 ));     // 2.05
console.log( toFixedIfNecessary( 2.05342, 2 ));  // 2.05
console.log( toFixedIfNecessary( 2.04999, 2 ));  // 2.05
console.log( toFixedIfNecessary( 2.04499, 2 ));  // 2.04
console.log( toFixedIfNecessary( 2.053435, 2 )); // 2.05
console.log( toFixedIfNecessary( 2.057435, 2 )); // 2.06

This is a modification of the answer by @MTO

parseFloat() is not necessary

Not the answer you're looking for? Browse other questions tagged or ask your own question.