-1

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?

Mouser
  • 13,132
  • 3
  • 28
  • 54

1 Answers1

2

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
  • 687,336
  • 108
  • 737
  • 1,005
  • 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