(UPDATE: To all those saying this is a dupe and voting to close, it is not a dupe of this question: the difference is that I want to display 1 dp numbers to 2 dp. It may well be a dupe of some other question, but please post a link if it is!)
I want to display a price in JavaScript. I would like to display an integer if the original number is a whole number. Or if it's a float, I would like to display it rounded to two decimal places.
input | output
24 | 24
24.231 | 24.23
24.2 | 24.20
I've tried Math.round(num * 100) / 100
, but that doesn't convert 24.2
correctly.
I've also tried .toFixed(2)
but that converts 24
to 24.00
, which isn't what I want.
Any ideas? This must be a very common problem when displaying prices.