[英]rounding variable and making it a currency
I am trying to get my outputs to round to two decimals places and have them formatted as a currency ie $23,923.23. 我试图将我的输出结果四舍五入到两位小数位,并将它们格式化为一种货币,即$ 23,923.23。 I am unfortunately struggling to get it to work.
不幸的是,我正在努力使其正常工作。 I am very new to this and have tried googling and searching on here for an answer but to no avail.
我对此很陌生,并尝试使用谷歌搜索并在此处搜索答案,但无济于事。 Any ideas?
有任何想法吗?
<script language="JavaScript">
<!--
function showpay() {
if ((document.calc.loan.value == null || document.calc.loan.value.length == 0) ||
(document.calc.months.value == null || document.calc.months.value.length == 0) ||
(document.calc.rate.value == null || document.calc.rate.value.length == 0)) {
document.calc.pay.value = "Incomplete data";
} else {
var princ = document.calc.loan.value;
var term = document.calc.months.value;
var intr = document.calc.rate.value / 1200;
var intr2 = document.calc.rate2.value / 1200;
document.calc.pay.value = princ * intr / (1 - (Math.pow(1 / (1 + intr), term)));
document.calc.pay2.value = princ * intr2 / (1 - (Math.pow(1 / (1 + intr2), term)));
var month1 = document.calc.pay.value;
var month2 = document.calc.pay2.value;
document.calc.difference.value = month1 - month2;
var difference = document.calc.difference.value;
document.calc.total.value = difference * term;
var intr3 = document.calc.rate3.value / 1200 + 1;
var intr4 = Math.pow(intr3, term)
document.calc.portfolio.value = difference * ((intr4 - 1) / (intr3 - 1));
}
// payment = principle * monthly interest/(1 - (1/(1+MonthlyInterest)*Months))
}
// -->
</script>
<form name=calc method=POST>
<table width=100% border=0>
<tr>
<th bgcolor="#aaaaaa" align=center>
<font color=blue>Student Loan Refinance Calculator</font>
</th>
</tr>
<tr>
<td bgcolor="#ffffff">
<font color=blue>Loan Amount <input type=number name=loan style="width: 100px"></td>
</tr>
<tr>
<td bgcolor="#ffffff">
<font color=blue>Loan Length in Months <input type=number name=months style="width: 75px"></td>
</tr>
<tr>
<td bgcolor="#ffffff">
<font color=blue>Old Interest Rate (7%=7) <input type=number name=rate style="width: 50px"></td>
</tr>
<tr>
<td bgcolor="#ffffff">
<font color=blue>New Interest Rate <input type=number name=rate2 style="width: 50px"></td>
</tr>
<tr>
<td bgcolor="#ffffff" align=center><input type=button onClick='showpay()' value=Calculate></td>
</tr>
<tr>
<td bgcolor="#ffffff">
<font color=blue>Old Monthly Payment <input type=number name=pay style="width: 100px"></td>
</tr>
<tr>
<td bgcolor="#ffffff">
<font color=blue>New Monthly Payment <input type=number name=pay2 style="width: 100px"></td>
</tr>
<tr>
<td bgcolor="#ffffff">
<font color=blue>Monthly Savings <input type=number name=difference style="width: 100px"></td>
</tr>
<tr>
<td bgcolor="#ffffff">
<font color=blue>Total Savings <input type=number name=total style="width: 100px"></td>
</tr>
<tr>
<th bgcolor="#aaaaaa" width=100%>
<font color=blue>If you invest the monthly savings</font>
</th>
</tr>
<tr>
<td bgcolor="#ffffff">
<font color=blue>Annual Return (7%=7) <input type=number name=rate3 style="width: 50px"></td>
</tr>
<tr>
<td bgcolor="#ffffff" align=center><input type=button onClick='showpay()' value=Calculate></td>
</tr>
<tr>
<td bgcolor="#ffffff">
<font color=blue>Portfolio Value <input type=number name=portfolio style="width: 100px"></td>
</tr>
</table>
</form>
Try the usage of AngularJS. 尝试使用AngularJS。 Here you have a currency-filter that formats automatically to a currency.
在这里,您有一个货币过滤器,该过滤器会自动格式化为货币。
将其舍入到两位小数,使用toFixed()函数。
n.toFixed(2)
Intl.NumberFormat can do all the work and be customized for specific countries. Intl.NumberFormat可以完成所有工作, 并可以针对特定国家/地区进行自定义。 This is just an example:
这只是一个例子:
var value = 1234567.809;
var locale = "en-au";
var options = {
style: "currency",
currency: "AUD",
minimumFractionDigits: 2,
maximumFractionDigits: 2
}
var nf = new Intl.NumberFormat(locale, options).format;
console.log( nf(value) ); //= $1,234,567.81
The downside is that it could lack support in browsers such as Safari, although there is a polyfill listed on github which overcomes this. 缺点是它可能缺乏对诸如Safari之类的浏览器的支持,尽管github上列出了一个polyfill可以克服这一问题。 Of course if the page is on an intranet that has standardized on a supporting browser, there is no problem.
当然,如果页面位于已在支持的浏览器上进行了标准化的Intranet上,则没有问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.