mickburkejnr
mickburkejnr

Reputation: 3690

How to stop JavaScript removing the 0 from a number after the decimal point?

I am using JQuery to do some calculations on some items that a user selects. One of the items which is priced at £13.95, when 2 are selected gives £27.90. However, the result is always displayed as £27.9, the sum removes the last 0.

How can I stop the Javascript doing this? Here is the code I am using - #pucMoH contains 13.95 as a string, which is then converted to a float:

var MoHCost = parseFloat($('#pucMoH').text()).toFixed(2);
var SelectedVal = parseFloat($(this).val()).toFixed(2);
var SelectedSum = MoHCost * SelectedValInt;

Upvotes: 1

Views: 706

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337560

The answer is in your question; toFixed(2):

var MoHCost = parseFloat($('#pucMoH').text()).toFixed(2);
var SelectedVal = parseFloat($(this).val()).toFixed(2);
var SelectedSum = (MoHCost * SelectedValInt).toFixed(2);

Upvotes: 3

Related Questions