繁体   English   中英

用JavaScript函数计算营业税

[英]Calculating Sales Tax in a javascript function

我在计算税款并将其添加到小计时遇到了麻烦。 如此一来,您就知道我对编码总体而言还是一个新手。 目标是将TirePrice,tireAmount和tireFees全部累加并征税,然后应将优惠券取走。 另外,如果您有任何改进的秘诀,请告诉我,但正如我所说的,请记住,我对此很陌生。 感谢您的时间 :)。

 function computePrice() { var tirePrice = document.getElementById('tirePrice').value; var tireAmount = document.getElementById('tireAmount').value; var tireFees = document.getElementById('tireFees').value; var tireCoupons = document.getElementById('tireCoupons').value; var finalPrice = ((+tirePrice * +tireAmount + +tireFees) * .8 - +tireCoupons).toFixed(2); finalPrice = finalPrice.toString().replace(/\\B(?=(\\d{3})+(?!\\d))/g, ","); document.getElementById('finalPrice').innerHTML = "Total:$" + finalPrice; } 
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>BJ's Tire Bay Calculator</title> <link rel="stylesheet" type="text/css" href="styles.css"> </head> <body> <h1>BJ's Tire Bay Calculator </h1> <p>Price Per Tire: $ <input id="tirePrice" type="number" min="1" max="100000"></p> <p>Number Of Tires: <input id="tireAmount" type="number" min="1" max="4"></p> <p>Installation Fee and NYS Recycling Fee: $<input id="tireFees" type="number" min="1" max="70"></p> <p>Coupons: $ <input id="tireCoupons" type="number" max="100000"></p> <input id="displayNum" value="Calculate" type="button" onclick="computePrice()"> <h2 id="finalPrice"></h2> </body> <script src="index.js"></script> </html> 

您需要先弄清楚如何计算营业税。 你可以看看其他的资源,但是这一次似乎给一个很好的例子。 还有包含性和排他性的营业税。 我不明白这一点-

一旦有了该数学,就可以执行类似的操作:

 const calculateSalesTax = (amount, taxPercent) => { // Whatever math is involved to calculate your sales tax here return amount + (taxPercent * amount); }; document.getElementById('calculate').onclick = () => { const amount = document.getElementById('amount').value; const tax = document.getElementById('percent').value; document.getElementById('total').innerText = `$ ${calculateSalesTax(+amount, +tax)}`; }; 
 <label for="amount">Amount</label> <input type="number" placeholder="amount" id="amount" /> <label for="percent">Sales Tax</label> <input type="number" placeholder="percent" id="percent" /> <button id="calculate">Calculate</button> <div id="total"></div> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM