matt_swinburne
asked on
Two decimal places
hello there from this code below i want the total cost to be rounded off to two decimal places and also how do you replace the caluclate text field with a button instead... Many thanks
<title>Example Calculation</title>
<script language= "JavaScript">
<!--
var entry = new Array()
function calcit()
{
entry[0]= (document.theform.ad_size. value)
entry[1]= (document.theform.Tier.val ue)
entry[2]= (document.theform.Host.val ue)
intval = entry[0]+entry[1]+entry[2]
document.theform.cost.valu e='£'+intv al
}
//-->
</script>
</head>
<body text="#000000" bgcolor="#FFFFFF">
<p>
<!--webbot bot="SaveDatabase" --></p>
<form name="theform" method="POST" action="--WEBBOT-SELF--">
<table width="900" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="300"><div align="center"><span style="margin-top: 0; margin-bottom: 6px">Number of Copies :
<!--webbot bot="Validation" b-value-required="TRUE"
b-disallow-first-item="TRU E" -->
<select
name="ad_size" size="1" id="ad_size">
<option value="0.00">Select Size</option>
<option value="80.00" selected>1</option>
<option value="9.00">2</option>
<option value="1.00">3</option>
<option value="2.00">4</option>
<option value="3.00">5</option>
<option value="4.00">6</option>
<option value="5.00">7</option>
<option value="6.00">8</option>
<option value="7.00">9</option>
<option value="8.00">10</option>
<option value="8.00">11</option>
<option value="3.00">12</option>
</select>
<font size="5" color="#FF0000">*</font></ span></div ></td>
<td width="300"><div align="center"><span style="margin-top: 0; margin-bottom: 6px"> Tier:
<!--webbot bot="Validation" b-value-required="TRUE"
b-disallow-first-item="TRU E" -->
<select name="Tier" size="1" id="Tier">
<option selected value="0.00">Select Tier</option>
<option value="0.00">Tier 1</option>
<option value="10.00">Tier 3</option>
</select>
<font size="5" color="#FF0000">*</font></ span></div ></td>
<td width="300"><div align="center"><span style="margin-top: 0; margin-bottom: 6px">
<select name="Host" size="1" id="Host">
<option value="0.00">Hosting Options</option>
<option value="0.00">My Site</option>
<option value="3.00">GoNooz</optio n>
</select>
<font size="5" color="#FF0000">*</font></ span></div ></td>
</tr>
<tr>
<td width="300"><div align="center"><span style="margin-top: 0; margin-bottom: 6px">
<input type= "Calc"
onClick ="calcit()" value= "Click To Calculate">
</span></div></td>
<td width="300"><div align="center"><span style="margin-top: 0; margin-bottom: 6px">Your charge is
<!--webbot bot="Validation" s-data-type="Number" s-number-separators=",."
s-validation-constraint="G reater than or equal to" s-validation-value="0" -->
<input name="cost" type="text" id="cost" value="$ 00.00" size="11">
USD</span></div></td>
<td width="300"><div align="center"><span style="margin-top: 0; margin-bottom: 6px">
<input type="submit"
value="Submit" name="B12">
<input type="reset" value="Reset" name="B22">
</span></div></td>
</tr>
</table>
<input type="submit"
value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>
</body>
</html>
<title>Example Calculation</title>
<script language= "JavaScript">
<!--
var entry = new Array()
function calcit()
{
entry[0]= (document.theform.ad_size.
entry[1]= (document.theform.Tier.val
entry[2]= (document.theform.Host.val
intval = entry[0]+entry[1]+entry[2]
document.theform.cost.valu
}
//-->
</script>
</head>
<body text="#000000" bgcolor="#FFFFFF">
<p>
<!--webbot bot="SaveDatabase" --></p>
<form name="theform" method="POST" action="--WEBBOT-SELF--">
<table width="900" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="300"><div align="center"><span style="margin-top: 0; margin-bottom: 6px">Number of Copies :
<!--webbot bot="Validation" b-value-required="TRUE"
b-disallow-first-item="TRU
<select
name="ad_size" size="1" id="ad_size">
<option value="0.00">Select Size</option>
<option value="80.00" selected>1</option>
<option value="9.00">2</option>
<option value="1.00">3</option>
<option value="2.00">4</option>
<option value="3.00">5</option>
<option value="4.00">6</option>
<option value="5.00">7</option>
<option value="6.00">8</option>
<option value="7.00">9</option>
<option value="8.00">10</option>
<option value="8.00">11</option>
<option value="3.00">12</option>
</select>
<font size="5" color="#FF0000">*</font></
<td width="300"><div align="center"><span style="margin-top: 0; margin-bottom: 6px"> Tier:
<!--webbot bot="Validation" b-value-required="TRUE"
b-disallow-first-item="TRU
<select name="Tier" size="1" id="Tier">
<option selected value="0.00">Select Tier</option>
<option value="0.00">Tier 1</option>
<option value="10.00">Tier 3</option>
</select>
<font size="5" color="#FF0000">*</font></
<td width="300"><div align="center"><span style="margin-top: 0; margin-bottom: 6px">
<select name="Host" size="1" id="Host">
<option value="0.00">Hosting Options</option>
<option value="0.00">My Site</option>
<option value="3.00">GoNooz</optio
</select>
<font size="5" color="#FF0000">*</font></
</tr>
<tr>
<td width="300"><div align="center"><span style="margin-top: 0; margin-bottom: 6px">
<input type= "Calc"
onClick ="calcit()" value= "Click To Calculate">
</span></div></td>
<td width="300"><div align="center"><span style="margin-top: 0; margin-bottom: 6px">Your charge is
<!--webbot bot="Validation" s-data-type="Number" s-number-separators=",."
s-validation-constraint="G
<input name="cost" type="text" id="cost" value="$ 00.00" size="11">
USD</span></div></td>
<td width="300"><div align="center"><span style="margin-top: 0; margin-bottom: 6px">
<input type="submit"
value="Submit" name="B12">
<input type="reset" value="Reset" name="B22">
</span></div></td>
</tr>
</table>
<input type="submit"
value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>
</body>
</html>
ASKER
When i insert this code it doesn't seem to work... Cheers. also do you know hoe to replace the calculation field with a button.... Thanks alot
And to make it work in all browsers:
function formatNumber (num, decplaces) { // JavaScript & DHTML Cookbook, by Danny Goodman.
// convert in case it arrives as a string value
num = parseFloat(num);
// make sure it passes conversion
if (!isNaN(num)) {
// multiply value by 10 to the decplaces power;
// round the result to the nearest integer;
// convert the result to a string
var str = "" + Math.round (eval(num) * Math.pow(10,decplaces));
// exponent means value is too big or small for this routine
if (str.indexOf("e") != -1) {
return "Out of Range";
}
// if needed for small values, pad zeros
// to the left of the number
while (str.length <= decplaces) {
str = "0" + str;
}
// calculate decimal point position
var decpoint = str.length - decplaces;
// assemble final result from: (a) the string up to the position of
// the decimal point; (b) the decimal point; and (c) the balance
// of the string. Return finished product.
return str.substring(0,decpoint) + "." + str.substring(decpoint,str .length);
} else {
return "NaN";
}
}
var entry = new Array()
function calcit()
{
entry[0]= parseFloat(document.thefor m.ad_size. options[do cument.the form.ad_si ze.selecte dIndex].va lue)
entry[1]= parseFloat(document.thefor m.Tier.opt ions[docum ent.thefor m.Tier.sel ectedIndex ].value)
entry[2]= parseFloat(document.thefor m.Host.opt ions[docum ent.thefor m.Host.sel ectedIndex ].value)
intval = entry[0]+entry[1]+entry[2]
document.theform.cost.valu e='£'+((in tval.toFix ed)?intval .toFixed(2 ):formatNu mber(intva l,2))
}
toFixed only works on numbers. You had strings - parseFloat fixed that
function formatNumber (num, decplaces) { // JavaScript & DHTML Cookbook, by Danny Goodman.
// convert in case it arrives as a string value
num = parseFloat(num);
// make sure it passes conversion
if (!isNaN(num)) {
// multiply value by 10 to the decplaces power;
// round the result to the nearest integer;
// convert the result to a string
var str = "" + Math.round (eval(num) * Math.pow(10,decplaces));
// exponent means value is too big or small for this routine
if (str.indexOf("e") != -1) {
return "Out of Range";
}
// if needed for small values, pad zeros
// to the left of the number
while (str.length <= decplaces) {
str = "0" + str;
}
// calculate decimal point position
var decpoint = str.length - decplaces;
// assemble final result from: (a) the string up to the position of
// the decimal point; (b) the decimal point; and (c) the balance
// of the string. Return finished product.
return str.substring(0,decpoint) + "." + str.substring(decpoint,str
} else {
return "NaN";
}
}
var entry = new Array()
function calcit()
{
entry[0]= parseFloat(document.thefor
entry[1]= parseFloat(document.thefor
entry[2]= parseFloat(document.thefor
intval = entry[0]+entry[1]+entry[2]
document.theform.cost.valu
}
toFixed only works on numbers. You had strings - parseFloat fixed that
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
document.theform.cost.valu
-r-