Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

rounding decimals 1

Status
Not open for further replies.

riffy

Programmer
Mar 29, 2001
106
US
Hi guys,

I got my other problem to work, the calculator problem.
This time I want to know is there a way to have only 2 decimal places? I'm calculating costs and when I multiply 2 values together in a couple of my functions, the answers result in many digits after the decimal. So is there a method that can round to only 2 decimal places?
I don't think Math.round() will do it, since that only rounds to integers right?

One of my functions is:

function lamp_savings () {
document.cost.save_lamps.value = ((document.cost.ann_cost2.value) * (document.cost.watt_save2.value))
document.cost.save_lamps.blur()
}

Thanks,
Arif
 
[tt]
Code:
  function RoundDecimal(x) {
    var num = new String(x);

    // getting integer portion of num
    var int = num.substr(0, num.indexOf("."));
    
    // getting remainder
    var rem = num % int;
    
    if(rem < .5)
      return int;
      
    return ++int;
  }
[/tt]
I hope this helped! ;-)
- Casey Winans
 
instead of having onFocus=&quot;this.blur();&quot; in an text box,
can we put that in the function that it's calling?
like for example

<input type=&quot;text&quot; name=&quot;try_this&quot; size=&quot;6&quot; onFocus=&quot;calculate();&quot;>
and in calculate make it blur? or do i have to include it within the tag?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top