View Full Version : Redusing decimal to only two figures.

09-17-2011, 04:36 PM
This script is for car loan payments. I need a R simbol only before the monthly payment and only two figures after the decimal point. Can somebody pse help me with this. Thank you in advance.
Kees Meyer (keesmarcha)

<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";
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))


// -->

The results of this loan payment calculator are for comparison purposes only.
They will be a close approximation of actual loan
repayments if available at the terms entered, from a financial institution. This
is being
provided for you to plan your next loan application. To use, enter values
for the
Loan Amount, Number of Months for Loan, and the Interest Rate (e.g.
7.25), and
click the Calculate button. Clicking the Reset button will clear entered
<form name=calc method=POST>
<table width=60% border=0>
<tr><th bgcolor="#aaaaaa" width=50%><font color=blue>Description</font></th>
<th bgcolor="#aaaaaa" width=50%><font color=blue>Data Entry</font></th></tr>
<tr><td bgcolor="#eeeee">Loan Amount</td><td bgcolor="#aaeeaa" align=right><input
type=text name=loan
<tr><td bgcolor="#eeeee">Loan Length in Months</td><td bgcolor="#aaeeaa"
align=right><input type=text
name=months size=10></td></tr>
<tr><td bgcolor="#eeeee">Interest Rate</td><td bgcolor="#aaeeaa" align=right><input
type=text name=rate
<tr><td bgcolor="#eeeee">Monthly Payment</td><td bgcolor="#eeaaaa"
align=right><em>Calculated</em> <input
type=text name=pay size=10></td></tr>
<tr><td bgcolor="#aaeeaa"align=center><input type=button onClick='showpay()'
value=Calculate></td><td bgcolor="#eeeeaa" align=center><input type=reset
<font size=1>Enter only numeric values (no commas), using decimal points
where needed.<br>
Non-numeric values will cause errors.</font>

<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

09-17-2011, 04:49 PM
change your code line -

document.calc.pay.value = princ * intr / (1 - (Math.pow(1/(1 + intr), term)));


document.calc.pay.value = 'R ' + (princ * intr / (1 - (Math.pow(1/(1 + intr), term)))).toFixed(2);

Thanks & Regards,
Niral Soni

09-17-2011, 06:03 PM
The .toFixed(2) solution should work, but keep in mind that it rounds the answer.

If you need a truncated answer, the solution would be:

var tempValue = princ * intr / (1 - (Math.pow(1/(1 + intr), term)));
document.calc.pay.value = parseInt(tempValue * 100)/100;

Could be condensed to one line, but this maybe easier to understand.

Code is untested, but should be close.

09-17-2011, 08:07 PM
Thank you very very much it work fine. Without you 99% of us is lost.

09-17-2011, 08:09 PM
Thank you very much for your quick response. We really need people with knowhow to assist us

09-17-2011, 08:14 PM
The .toFixed(2) solution should work, but keep in mind that it rounds the answer. Thank you very much for your quick solution.