0

My MySQL DB stores and returns numbers with two decimal points, like 4 is 4.00. and 4.9 is 4.90.

But I want to format them in HTML or using javascript such that 4.00 is 4, and 4.90 is 4.9, 4.25 is 4.25 (get rid of the trailing zeros).

How do I do this?

Thanks

UPDATE: (Requirements)

After reading some of the response, I realize I need to provide more info due to the way the server side code returns data. The php returns a JSON string containing the numeric along with many other data, and then it is dumped into a jQuery template, with the following to receive the data

           <div class="duration">${duration}</div>
William Sham
  • 12,849
  • 11
  • 50
  • 67
  • 2
    Can you modify the server-side script that is retrieving the values from the database? If so, it is probably better to do that than use Javascript since it will be independent of the user's browser. – someone Sep 06 '11 at 20:31

3 Answers3

1

Use parseFloat.

e.g:

var d = "4.90";
alert(parseFloat(d));
Chandu
  • 81,493
  • 19
  • 133
  • 134
1

In Javascript, you can use Number("4.90") which will return 4.9. In PHP you could use echo ((double)"4.90").

someone
  • 1,468
  • 8
  • 9
0

Use a regex: see this SO post.

Community
  • 1
  • 1
JW8
  • 1,496
  • 5
  • 21
  • 36