28

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
Elfy
  • 1,733
  • 6
  • 19
  • 39
  • 2
    possible duplicate of [Round to at most 2 decimal places in JavaScript](http://stackoverflow.com/questions/11832914/round-to-at-most-2-decimal-places-in-javascript) – Vucko Aug 26 '15 at 14:42
  • but that also suggets I use toFixed(2) which doesn't work like I want – Elfy Aug 26 '15 at 14:43
  • So use the script from the most voted answer? Here's [your example](http://jsfiddle.net/af3qg9bc/) – Vucko Aug 26 '15 at 14:46

2 Answers2

55

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
MT0
  • 143,790
  • 11
  • 59
  • 117
  • 4
    What sworcery is this? Can someone explain why +parseFloat has this behaviour? – homebrand Dec 25 '17 at 08:33
  • 4
    @homebrand the + converts the string output of toFixed back to a float. Try `+"2.00"` in the console – Rani May 11 '19 at 15:47
  • 1
    This use case scenario is not covered: console.log( toFixedIfNecessary(0.000000032, 10 )); // 3.2e-8 – Geraldo B. Landre Aug 18 '21 at 05:49
  • This will convert `66.666` to `66.67`. – wintercounter Feb 10 '23 at 09:21
  • @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 Feb 10 '23 at 09:22
  • OP didn't ask to round it. – wintercounter Feb 11 '23 at 10:04
  • @wintercounter Implicitly, yes, they did as they asked for it to (at most) 2 decimal places which the common definition means that if there is more than 2 decimal places then it should be rounded to 2 decimal places. If you want a non-standard meaning of the term "to 2 d.p." then you need to specify that you not rounding and instead want to truncate, floor, ceiling, round half-up, etc. – MT0 Feb 11 '23 at 10:46
  • @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](https://stackoverflow.com/q/1685680/1509264) such as [this answer](https://stackoverflow.com/a/36028587/1509264) (and my comment on it). – MT0 Feb 11 '23 at 11:09
  • I think it's worth making it explicit in the answer that in cases like this, the function may not behave as expected. `toFixed`'s definition says it "formats a number using fixed-point notation." https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed Therefore, the reader is possibly expecting the method to be returning a string with the format requested in the question. If it's not possible to accomplish what is being asked for all cases, then, that is an important part of the answer, in my opinion. – Geraldo B. Landre Feb 16 '23 at 02:24
2

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
baao
  • 71,625
  • 17
  • 143
  • 203
  • `Math.round( 1.005 * 100 ) / 100` returns `1` – MT0 Aug 26 '15 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 – Neven Ignjic Aug 26 '15 at 14:56
  • 3
    @NevenIgnjic `1.005` to 2dp is `1.01` not `1` – MT0 Aug 26 '15 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. – Neven Ignjic Aug 26 '15 at 15:01