Jquery在val中显示小数



我有下面的语法,每个函数设置输入框的值。

折扣,小数点后1位调整后的价格,小数点后2位(货币)

如何显示每个函数的小数位

   function calculateRow(row) {
        var price = +row.find('input[name^="adjustedprice"]').val();
        var qty = +row.find('input[name^="qty"]').val();
        row.find('input[name^="linetotal"]').val((price * qty).toFixed(2));
        var listprice = +row.find('input[name^="price"]').val();
        var discount = 0;
        var discount = Math.round(100 - (price / listprice) * 100);
        row.find('input[name^="discount"]').val(discount);
    }
    function calculateAjustedPrice(row) {
        var listprice = +row.find('input[name^="price"]').val();
        var discount = +row.find('input[name^="discount"]').val();
        var adjustedprice = Math.round((listprice/100)*(100-discount));
        row.find('input[name^="adjustedprice"]').val(adjustedprice);
        calculateRow(row);
        calculateGrandTotal();
    }

您可以使用toFixed方法设置使用定点记数法的小数点:

(number).toFixed(n); 
// where number is the value you want to format
// n is the decimal places you want
例如

var n = 12.8;
n.toFixed(2); // Returns 12.80 : note added zero

最新更新