开发者

Format numeric returned by MySQL using javascript or HTML

开发者 https://www.devze.com 2023-04-02 17:33 出处:网络
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.

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)

开发者_如何学JAVA

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>


Use parseFloat.

e.g:

var d = "4.90";
alert(parseFloat(d));


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


Use a regex: see this SO post.

0

精彩评论

暂无评论...
验证码 换一张
取 消