Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQueryMath返回0.0,但需要0.00_Jquery_Math_Numbers_Dollar Sign - Fatal编程技术网

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()
函数是一个更好的解决方案。