0

javascript. How to nicely format floating numbers to string without unnecessary decimal 0?

Here's an example input:

  • 12.000
  • 12.354
  • 12.340
  • 12.700

What I want is:

  • 12
  • 12.354
  • 12.34
  • 12.7
Huangism
  • 16,278
  • 7
  • 48
  • 74
  • 3
    How do you currently *format* ? You should not have any 0 unless you add them. – Denys Séguret Oct 27 '14 at 14:38
  • What *exactly* are you trying to do here? Round to 3 d.p. but only display those d.p. if they are non-zero? If the input was `12.3545` what would you expect the output to be? Are you inputs really numbers? Or are they strings? – Matt Burland Oct 27 '14 at 14:50
  • possible duplicate of [How to format a float in javascript?](http://stackoverflow.com/questions/661562/how-to-format-a-float-in-javascript) – Alexander Oct 27 '14 at 15:26

1 Answers1

1

Use parseFloat - it will take care of any floating 0

alert(parseFloat("12.00")); //12
tymeJV
  • 103,943
  • 14
  • 161
  • 157