Link to home
Start Free TrialLog in
Avatar of matt_swinburne
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.value)
entry[2]= (document.theform.Host.value)
intval = entry[0]+entry[1]+entry[2]
document.theform.cost.value='£'+intval
}
//-->
</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 :&nbsp;
          <!--webbot bot="Validation" b-value-required="TRUE"
  b-disallow-first-item="TRUE" -->
          <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:&nbsp;
          <!--webbot bot="Validation" b-value-required="TRUE"
  b-disallow-first-item="TRUE" -->
          <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</option>
        </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="Greater 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>
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Use:

document.theform.cost.value='£'+intval.toFixed(2);

-r-
Avatar of matt_swinburne
matt_swinburne

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.theform.ad_size.options[document.theform.ad_size.selectedIndex].value)
entry[1]= parseFloat(document.theform.Tier.options[document.theform.Tier.selectedIndex].value)
entry[2]= parseFloat(document.theform.Host.options[document.theform.Host.selectedIndex].value)
intval = entry[0]+entry[1]+entry[2]
document.theform.cost.value='£'+((intval.toFixed)?intval.toFixed(2):formatNumber(intval,2))
}

toFixed only works on numbers. You had strings - parseFloat fixed that
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial