javascript - How to Display Commas After Every Third Number Without Compromising Calculator -
i trying create loan calculator uses commas separate every third number. example, $1,000,000.75.
is there way display of input values this, without compromising actual calculation of numbers?
right now, if comma entered in of inputs, calculated input (input displays calculation), throws error (nan). wondering if there way using such php or javascript?
here picture of working example of loan calculator:
here full page code loan calculator:
<!doctype html> <html> <head> <style> body { font-family:arial,verdana,sans-serif; } img { border:none; } img { border:none; } .bback { text-align:center; width:100%; } #image { width:84px; height:41px; } #stretchtable { width:60%; max-width:500px; min-width:200px; } .fontwhite { color:white; background-color:black; border:4px grey solid; padding:5px; text-align:left; } </style> <meta charset="utf-8"> <title> ::: loan calculator</title> </head> <body bgcolor="#102540"> <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; princ = princ.replace(',',''); var myfloat = parsefloat(princ); var term = document.calc.months.value; term = term.replace(',',''); var myfloat1 = parsefloat(term); var intr = document.calc.rate.value / 1200; intr = intr.replace(',',''); var myfloat2 = parsefloat(intr); document.calc.pay.value = (myfloat * myfloat2 / (1 - (math.pow(1/(1 + myfloat2), myfloat1)))).tofixed(2) } // payment = principle * monthly interest/(1 - (1/(1+monthlyinterest)*months)) } // --> </script> <script> function trimdp(x, dp) { x = parsefloat(x); if (dp === 0) return math.floor(x).tostring(); dp = math.pow(10, dp || 2); return (math.floor((x) * dp) / dp).tostring(); } window.addeventlistener('load', function () { var nodes = document.queryselectorall('.dp2'), i; function press(e) { var s = string.fromcharcode(e.keycode); if (s === '.') if (this.value.indexof('.') === -1) return; // permit typing `.` this.value = trimdp(this.value + s); e.preventdefault(); }; function change() { this.value = trimdp(this.value); } (i = 0; < nodes.length; ++i) { nodes[i].addeventlistener('keypress', press); nodes[i].addeventlistener('change', change); } }); </script> <div class="bback"> <h1 style="color:white;font-size:16px;">g.b.m. trailer service ltd. loan calculator</h1> <a href="index.html"> <img src="images/backbutton.png" alt="back button" id="image" title="back"></a><br /><br /> <center> <div class="fontwhite" style="width:60%;"> results of loan payment calculator comparison purposes only. close approximation of actual loan repayments if available @ terms entered, financial institution. being provided plan next loan application. use, enter values loan amount, number of months loan, , interest rate (e.g. 7.25), , click calculate button. clicking reset button clear entered values. </div> </center> </div> <p> <center> <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" onkeyup="format(this)"></td></tr> <tr><td bgcolor="black">loan length in months</td><td bgcolor="black" align=center><input type=text name=months size=10 onkeyup="format(this)"></td></tr> <tr><td bgcolor="black">interest rate</td><td bgcolor="black" align=center><input type=text name=rate size=10 onkeyup="format(this)"></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" onkeyup="format(this)"></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> <div style="width:60%;"> <font size=2 color=white>enter numeric values (no commas), using decimal points needed.<br> non-numeric values cause errors.</font> </center> </div> <p align="center"><font face="arial" size="-2">this free script provided by</font><br> <font face="arial, helvetica" size="-2"><a href="http://javascriptkit.com">javascript kit</a></font></p> </body> </html>
i looking solution problem, not experienced type of code. suggestions may me far.
thank help. appreciated.
replace this:
var intr = document.calc.rate.value / 1200;
with
var intr = (parsefloat(document.calc.rate.value) / 1200).tostring()
for adding commas bit, replace this:
document.calc.pay.value = (princ * intr / (1 - (math.pow(1/(1 + intr), term)))).tofixed(2)
with this
document.calc.pay.value = (princ * intr / (1 - (math.pow(1/(1 + intr), term)))).tofixed(2).tolocalestring()
there other ways it, seems fastest way without introducing more functions. javascript gives tolocalestring should flexible option.
Comments
Post a Comment