十进制值添加无法在javascript中工作

时间:2013-12-24 09:48:13

标签: javascript

function sum() {
     a = Number(document.getElementById("rate_ts").value);
     b = Number(document.getElementById("discount").value);
     c = a - (Number(a) * Number(b) / 100);
     d = Number(document.getElementById("ocharge").value) + c + Number(document.getElementById("pay_amt").value);
     tax = (Number(a) * Number(12.36) / 100);
     e = tax + d;
     document.getElementById("net_amt").value = e;
 }

这段代码工作正常......我想加税,为此我给了12.36,但它没有返回任何东西..plz help

2 个答案:

答案 0 :(得分:0)

试试这个,

function sum() {
     a = parseFloat(document.getElementById("rate_ts").value);
     b = parseFloat(document.getElementById("discount").value);
     c = a - (a * b / 100);
     d = parseFloat(document.getElementById("ocharge").value) + c + parseFloat(document.getElementById("pay_amt").value);
     tax = a * 12.36 / 100;
     e = tax + d;
     document.getElementById("net_amt").value = e.toFixed(2);
 }

如果您只想要两个小数点值,请使用number.toFixed(2)

<强> Working Example

答案 1 :(得分:0)

parseFloat()

  

摘要

     

parseFloat()函数解析字符串参数并返回一个   浮点数。

$('#three').click(function (e) {
     a = parseFloat(document.getElementById("rate_ts").value);
     b = parseFloat(document.getElementById("discount").value);

    console.log(a+' | '+b);
});

WORKING DEMO