简体   繁体   English

如何使输入舍入到最近的2个小数位

[英]How to Make Input Round to the Nearest 2 Decimal Places

I am trying to create a loan calculator that will give me the calculated monthly payment in a number with a maximum of two decimal places. 我正在尝试创建一个贷款计算器,该计算器将为我提供计算得出的每月还款额,最多为两位小数。 I am currently using the following formula, but it gives me more than two decimal places (see the image below.): 我目前正在使用以下公式,但是它给了我两个以上的小数位数(请参见下图):

<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;
 document.calc.pay.value = princ * intr / (1 - (Math.pow(1/(1 + intr), term)));
 }

// payment = principle * monthly interest/(1 - (1/(1+MonthlyInterest)*Months))

}

// -->
</script>

Here is the HTML for my loan calculator: 这是我的贷款计算器的HTML:

<form name=calc method=POST>
<div style="color:white; font-weight:bold; border:4px grey outset; padding:0px; margin:0px;" id="stretchtable">
<table width="100%" border="1" style="border:1px outset grey">
<tr><th bgcolor="black" width=50%><font color=white>Description</font></th>
<th bgcolor="black" width=50%><font color=white>Data Entry</font></th></tr>
<tr><td bgcolor="black">Loan Amount</td><td bgcolor="black" align=center><input
type=text name=loan
size=10 class="dp2"></td></tr>
<tr><td bgcolor="black">Loan Length in Months</td><td bgcolor="black"
align=center><input type=text
name=months size=10></td></tr>
<tr><td bgcolor="black">Interest Rate</td><td bgcolor="black" align=center><input
type=text name=rate
size=10></td></tr>
<tr><td bgcolor="black">Monthly Payment</td><td bgcolor="black"
align=center><em>Calculated</em> <input
type=text name=pay size=10 class="dp2"></td></tr>
<tr><td  bgcolor="black"align=center><input type=button onClick='showpay()'
value=Calculate></td><td bgcolor="black" align=center><input type=reset
value=Reset></td></tr>
</table>
</div>
</form>

When the calculated monthly payment appears, it appears in many decimal places. 出现计算的每月付款时,它会显示在许多小数位。 I would like this to be limited to only 2 decimal places, and for those decimal places to be rounded to the nearest hundredth. 我希望将其限制为仅保留2个小数位,并且将那些小数位四舍五入到最接近的百分之一。 I believe this can be achieved through PHP or JavaScript. 我相信可以通过PHP或JavaScript来实现。

Note: I am looking for a direct solution to my problem, not suggestions/hints. 注意:我正在寻找直接解决问题的方法,而不是建议/提示。

For example: 例如:

If I had 2.489999 as the monthly calculated payment, than the new code would make it 2.49 . 如果我有2.489999作为每月计算的付款,那么新代码将使其2.489999 2.49

Here is a picture of what my loan calculator looks like: 这是我的贷款计算器的外观图片:

在此处输入图片说明

Thank you for any help. 感谢您的任何帮助。 All help is greatly appreciated. 非常感谢所有帮助。

Try this one in javascript 在javascript中尝试这个

num.toFixed(2),

where num is the number you want to round. 其中num是您要四舍五入的数字。

and the following one in PHP: 和以下PHP中的一个:

round(num,2)

In php 在PHP中

 round(2.489999, 2)

In javascript 在JavaScript中

 2.489999.toFixed(2)

Both round the number to 2 decimal places. 两者都将数字四舍五入到小数点后两位。

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

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