Only Bolivian Here
Only Bolivian Here

Reputation: 36753

Javascript value doesn't display decimals unless it's something other than 0.00

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

Upvotes: 7

Views: 10100

Answers (3)

Variant
Variant

Reputation: 17375

use:

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

Upvotes: 13

Ben
Ben

Reputation: 62444

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

Upvotes: 4

Phrogz
Phrogz

Reputation: 303361

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

Upvotes: 3

Related Questions