Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
35
rated 0 times [  41] [ 6]  / answers: 1 / hits: 29197  / 14 Years ago, wed, january 5, 2011, 12:00:00

Give me a native (no jQuery, Prototype, etc. please) JavaScript function that converts numbers as follows:



input:  0.39, 2.5,  4.25, 5.5,  6.75, 7.75, 8.5
output: 0.39, 2.50, 4.25, 5.50, 6.75, 7.75, 8.50


E.g., in Ruby, I'd do something like this:



>> sprintf(%.2f, 2.5)
=> 2.50


The output may be a number or a string. I don't really care because I'm just using it to set innerHTML.



Thank you.


More From » numbers

 Answers
17
input = 0.3;
output = input.toFixed(2);
//output: 0.30

[#94359] Tuesday, January 4, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
keyonnal

Total Points: 746
Total Questions: 103
Total Answers: 116

Location: Moldova
Member since Sat, Aug 6, 2022
2 Years ago
;