I want to format a number to two decimal places. Say the user enters 8764444 it should be formatted as 8.76. is there some built-in function in javascript to do that?
Asked
Active
Viewed 379 times
-2
Mouser
- 12,995
- 3
- 27
- 51
user3791256
- 11
- 4
1 Answers
1
No, there is no built in method for exactly that, but you can use the substr method to get parts of a string to do the formatting:
var input = "8764444";
input = input.substr(0, 1) + '.' + input.substr(1, 2);
// show result in Stackoverflow snippet
document.write(input);
Guffa
- 666,277
- 106
- 705
- 986
-
Why the downvote? If you don't explain what it is that you think is wrong, it can't improve the answer. – Guffa Jan 25 '15 at 12:21