...

View Full Version : Another "how to round" question



Wonderland
05-27-2004, 10:51 PM
I have read the rounding forums but am still unsure how to incorporate it into my code.

function calculateResult(){
var pcintX = document.form1.x.value;
var pcintY = document.form1.y.value;
var pcintZ = ((pcintX * pcintX)*90.69)/(pcintY * pcintY);
document.form1.z.value = pcintZ;
return false;
}

I need the calculation to round the pcintZ value to 2 decimal places.

Any help on how I could do this?

Thank you

brothercake
05-27-2004, 11:05 PM
Rounding something to two digits is the same as multiply it by 100, rounding it to an integer, then dividing it by 100 again. Like this:


pcintZ = Math.round(pcintZ * 100) / 100;

liorean
05-27-2004, 11:14 PM
If you want it to always display two decimals (instead of dropping ending zeroes), you can have a look in the FAQ. You can find a function for doing that in there.

Wonderland
05-27-2004, 11:58 PM
Great thank you! - Problem solved.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum