Solved

Two decimal places

Posted on 2006-06-23
4
1,576 Views
Last Modified: 2012-06-21
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>
0
Comment
Question by:matt_swinburne
  • 2
4 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 16967419
Use:

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

-r-
0
 

Author Comment

by:matt_swinburne
ID: 16967460
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
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 16967483
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
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 16967495
<input type= "button"
  onClick ="calcit()" value= "Click To Calculate">
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now