最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

floating point - Javascript value doesn't display decimals unless it's something other than 0.00 - Stack Overflo

programmeradmin4浏览0评论
//Value being parsed at this point is "150.00"
var productValue = parseFloat($("#product-cost-value-value").text().replace(',', '.'));    

console.log(productValue);

The value being logged is 150.

However if I add some value to it, like this. The value changes to display the two decimals I want.

console.log(productValue + 0.01);

The value being logged is 150.01

How can I force my values to show two decimal places at all time?

//Value being parsed at this point is "150.00"
var productValue = parseFloat($("#product-cost-value-value").text().replace(',', '.'));    

console.log(productValue);

The value being logged is 150.

However if I add some value to it, like this. The value changes to display the two decimals I want.

console.log(productValue + 0.01);

The value being logged is 150.01

How can I force my values to show two decimal places at all time?

Share Improve this question edited Jun 18, 2012 at 13:39 mskfisher 3,4064 gold badges37 silver badges49 bronze badges asked Nov 8, 2011 at 13:49 Only Bolivian HereOnly Bolivian Here 36.7k65 gold badges166 silver badges257 bronze badges 2
  • possible duplicate of How to format a float in javascript? – Dave Newton Commented Nov 8, 2011 at 13:51
  • possible duplicate of trim to 2 decimals – Phrogz Commented Nov 8, 2011 at 13:52
Add a comment  | 

3 Answers 3

Reset to default 13

use:

console.log(productValue.toFixed(2));

Use productValue.toFixed(2) to show 2 decimal places

Use Number.prototype.toFixed(2) (e.g. productValue.toFixed(2)) to convert your number into a string formatted to two decimal places.

发布评论

评论列表(0)

  1. 暂无评论