Click to See Complete Forum and Search --> : Anybody good with Java Calculators?


jamemiddz
10-16-2003, 07:25 AM
Can anyone help me out with my script?
Ive put this together, I just a begginers with all this javascript stuff. I need to cause the final figure to read out in £'s and pence, i.e. £35.50 and not £35.5. I think that I need to incorporate another 'MATH' command, but I'm not sure how to......any ideas?

(this bit goes into the head)

<SCRIPT language=JavaScript>
/*
James Middleton - calculator
*/
<!--

function perc1() {

/* get value of property */
propertypc = document.form1.propertypc.value/100;
propertyval = propertypc*document.form1.propertyval.value

/* get value of contents */
contentpc = document.form1.contentpc.value/100;
contentval = contentpc*document.form1.contentval.value;

bothval = propertyval + contentval

premium = bothval / 100 * 5 + bothval

premium = Math.round(premium);

document.form1.final.value = "£" + premium

}


//-->
</script>


(This bit goes into the body)

<table width="400" border="0" cellspacing="0" cellpadding="10" height="250"><tr>
<td colspan="2" background="../images/darkgreen.gif">
<div align="right"><B><span class="Heading">Insurance Calculator</span></B></div></td></tr><tr>
<td width="50%" background="../images/lightgreen.gif">
<div align="right">Property Value = <input type="hidden" value="0.18" border="0" name=propertypc></div></td>
<td width="50%" background="../images/darkgreen.gif">&pound; <INPUT size=10 name=propertyval tabindex="1"> i.e. 100000</td>
</tr><tr><td width="50%" background="../images/lightgreen.gif">
<div align="right">Content Value = <input type="hidden" value="0.6" border="0" name=contentpc></div></td>
<td width="50%" background="../images/darkgreen.gif">&pound; <INPUT size=10 name=contentval> i.e. 6000</td></tr><tr>
<td width="50%" background="../images/lightgreen.gif">
<div align="right"><input onclick=perc1() type=button value=Calculate><INPUT type=reset value=Reset></div>
</td><td width="50%" background="../images/darkgreen.gif"></td></tr><tr>
<td width="50%" background="../images/lightgreen.gif">
<div align="right">Total Annual Payments<br>(inclusive of 5% Insurance Premium)</div></td><td width="50%" background="../images/darkgreen.gif">&pound; <input maxlength=40 size=10 name=final></td></tr></table>
</td><td valign="top"><div align="right"></div></td></tr>
</table></FORM></td></tr><tr></tr></table><p></p>

Thanks for checking it for me.
James Middleton

pyro
10-16-2003, 07:37 AM
Look into toFixed() (http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/number.html#1200964)

lillu
10-16-2003, 07:42 AM
The pow() method takes two parameters, the first raises a number to a specified power,
(raising the number to the 10th power in the example). The second parameter is number of decimals
you want displayed. (You pass 2 to the function obviously.)


<SCRIPT language=JavaScript>
function showDec(myNumber, numberOfDec)
{
var corrDec = Math.pow(10,numberOfDec);
myNumber = Math.round(myNumber * corrDec) / corrDec;
return myNumber;
}
</SCRIPT>