如何阻止JavaScript从小数点后的数字中删除0?

时间:2021-02-08 17:08:47

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.

我正在使用JQuery对用户选择的一些项目进行一些计算。其中一件售价为13.95英镑,当选择2件时售价为27.90英镑。但是,结果始终显示为£27.9,总和将删除最后的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:

如何阻止Javascript执行此操作?这是我正在使用的代码 - #pucMoH包含13.95作为字符串,然后转换为float:

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

1 个解决方案

#1


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

答案在你的问题中; toFixed(2):

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

#1


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

答案在你的问题中; toFixed(2):

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