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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JavaScript, rounding numbers

Status
Not open for further replies.

gelthie

Programmer
Oct 17, 2001
10
US
How can I round off numbers, like for the example below;

<title>JavaScript Calculation</title>
</head>
<body>
<script Language = &quot;JavaScript&quot;>
var farentemp
farentemp=prompt(&quot;Please enter a temperature in Fahrenheit.&quot;,&quot;&quot;);
var celtemp
celtemp=(5/9)*(farentemp-32)
document.write(&quot;In Celsius, the temperature is &quot;);
document.write(celtemp);
document.write(&quot; degrees!&quot;);
</script>

if I put in a temparature of 24 degrees Farenheit, it gives me something like -4.444444444444445. I am trying to figure out how to round it off to 2 decimals.
thanks
 
ok here is one way though there are probably loads of ways of approaching this one...

var num = 1.234567

function roundNum(num){
n = num*100;
roundednum = Math.round(n);
num = roundednum/100;
}

num would now equal 1.23

hope this helps

rob
 
oh if you wanted to round off to three decimaals then you would multiply and divide by 1000 and so on....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top