I'm currently displaying a quantity in Javascript. I want it to display (for example) the whole number as 89
instead of 89.00
; however, if the number is fractional like 89.50
then it should display 89.50
Asked
Active
Viewed 225 times
1

Michael Mrozek
- 169,610
- 28
- 168
- 175

user338393
- 11
- 1
-
possible duplicate of [Javascript printf/string.format](http://stackoverflow.com/questions/610406/javascript-printf-string-format) – Michael Mrozek May 11 '10 at 15:00
1 Answers
2
It isn't clear if your question is about parsing floats or about checking integers. Here is a function that accepts numbers or strings and returns the float formatted as a string:
function displayQuantity(n) {
return parseFloat(n).toFixed(n%1 ? 2 : 0);
}
In case you have to support (very) old browsers, look here for an implementation of Number#toFixed
.