7
//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?

mskfisher
  • 3,291
  • 4
  • 35
  • 48
Only Bolivian Here
  • 35,719
  • 63
  • 161
  • 257
  • possible duplicate of [How to format a float in javascript?](http://stackoverflow.com/questions/661562/how-to-format-a-float-in-javascript) – Dave Newton Nov 08 '11 at 13:51
  • possible duplicate of [trim to 2 decimals](http://stackoverflow.com/questions/6525335/trim-to-2-decimals) – Phrogz Nov 08 '11 at 13:52

3 Answers3

13

use:

console.log(productValue.toFixed(2));
Variant
  • 17,279
  • 4
  • 40
  • 65
4

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

Ben
  • 60,438
  • 111
  • 314
  • 488
3

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

Phrogz
  • 296,393
  • 112
  • 651
  • 745