jQueryMath返回0.0,但需要0.00
我有以下功能:jQueryMath返回0.0,但需要0.00,jquery,math,numbers,dollar-sign,Jquery,Math,Numbers,Dollar Sign,我有以下功能: function checktotal(){ total = currentproduct["priceperticket"] * $("#qtyselect").val(); $("span#totalprice").html("$"+total); } 当总数为65.50时,返回65.5 我不知道是否需要在处执行长度或拆分,然后加入0或执行什么操作。number.toString(),然后是rpad 使用'toFixed'方法: $("span#totalp
function checktotal(){
total = currentproduct["priceperticket"] * $("#qtyselect").val();
$("span#totalprice").html("$"+total);
}
当总数为65.50时,返回65.5
我不知道是否需要在
处执行长度或拆分,然后加入0或执行什么操作。number.toString(),然后是rpad
使用
'toFixed'
方法:
$("span#totalprice").html("$"+total.toFixed(2));
尝试使用.toFixed(2)
。见下文
function checktotal(){
total = parseFloat(currentproduct["priceperticket"] * $("#qtyselect").val()).toFixed(2);
$("span#totalprice").html("$"+total);
}
根据项目的性质,您可能需要考虑使用./P> 实现目标的一个简单方法是使用函数,该函数将以2位小数呈现数字,但在执行算术之前,还应检查
NaN
值
function checktotal(){
var price = parseFloat(currentproduct["priceperticket"]);
var quantity = parseFloat($("#qtyselect").val());
// Check if price or quantity is not a number
// IF so, clear the price display (or do something else)
if (isNaN(price) || isNaN(quantity))
$("span#totalprice").html("");
var total = (price * quantity).toFixed(2);
$("span#totalprice").html("$" + total);
}
请检查这个
parseFloat("65.5").toFixed(2)
使用内置的
.toFixed()
函数是一个更好的解决方案。