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,177
  • 3
  • 33
  • 47
Only Bolivian Here
  • 34,179
  • 61
  • 156
  • 252
  • 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
  • 16,901
  • 4
  • 38
  • 65
4

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

Ben
  • 58,238
  • 105
  • 295
  • 471
3

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

Phrogz
  • 284,740
  • 104
  • 634
  • 722