0

我有以下功能:

function checktotal(){
    total = currentproduct["priceperticket"] * $("#qtyselect").val();
    $("span#totalprice").html("$"+total);
}

当它总共有 65.50 时,它返回 65.5。

我不知道我是否需要在 处做一个长度或拆分,.然后加入一个 0 或做什么。

4

5 回答 5

3

使用'toFixed'方法:

$("span#totalprice").html("$"+total.toFixed(2));
于 2012-03-22T20:06:39.473 回答
1

number.toString(),然后是 rpad http://ksblog.org/index.php?q=lpad-rpad-functions-javascript&id=44

于 2012-03-22T20:06:00.020 回答
1

尝试使用.toFixed(2). 见下文,

function checktotal(){
    total = parseFloat(currentproduct["priceperticket"] * $("#qtyselect").val()).toFixed(2);
    $("span#totalprice").html("$"+total);
}
于 2012-03-22T20:07:44.613 回答
1

根据您项目的性质,您可能需要考虑使用accounting.js

实现目标的一种简单方法是使用.toFixed()将数字显示为小数点后 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);
}
于 2012-03-22T20:08:30.610 回答
1

请检查这个

   parseFloat("65.5").toFixed(2)
于 2012-03-22T20:27:25.703 回答