Mortgage Example
WML Code(mortgage.wml)
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card id="card1" title="Mortgage Calc" newcontext="true">
<p>
Principal: <input format="N*N" name="principal" title="Principal:" value="150000"/>
<br/>
Interest: <input format="N*M" name="interest" title="Interest:" value="8.00"/>
<br/>
Num payments:<input format="N*N" name="num_payments" title="Num. payments:" value="360"/>
<br/>
Payment = <u>$(payment)</u>
<do type="accept" label="Calculate">
<go href="mortgage.wmls#payment('payment','$(principal)','$(interest)','$(num_payments)')"/>
</do>
<do type="help" label="Help">
<go href="#help"/>
</do>
</p>
</card>
<card id="help" title="Help">
<p>
<u>Principal</u> - $$ amount<br/>
<u>Interest</u> - e.g. 7.5<br/>
<u>Payments</u> - e.g. 360 for 30 years<br/>
<do type="prev" label="Back">
<prev/>
</do>
</p>
</card>
</wml>
WMLScript Code (mortgage.wmls)
/*
* Calculate a mortgage's payment
*
*@param varname the variable name to store the result
*@param principal the principal
*@param interest the interest rate
*@param num_payments the number of payements
*@return the payment
*/
extern function payment(varname, principal, interest, num_payments) {
/*
* Interest formulae:
*
* If (i != 0), then:
* pmt = principal * [i * (1+i)^n / ((1+i)^n - 1)]
*
* If (i == 0), then:
* pmt = principal / n
*/
var mi = interest/1200; // monthly interest from annual percentage
var payment = 0;
if (mi != 0) {
var tmp = Float.pow((1 + mi), num_payments);
payment = principal * (mi * tmp / (tmp - 1));
} else {
if (num_payments != 0)
payment = principal / num_payments;
}
var s;
if (payment != 0)
s = String.format("$%6.2f", payment);
else
s = "Missing data";
/*
* Send the result to the browser
*/
WMLBrowser.setVar(varname, s);
/*
* Make sure the browser updates its current card
*/
WMLBrowser.refresh();
};