...

View Full Version : Rounding numbers after calculations



marvcollier
07-26-2002, 10:46 PM
I am trying to round up numbers after a calculation so only 2 digits to the right of the decimal will remain. I am using javascript for the calculation on my web form. I have some equations that multiply by numbers like ".000038"; Can someone help me with the code that will round the numbers to my specifications?


Thank you,

Marvin

BrightNail
07-26-2002, 11:21 PM
try this..


function roundOff(value)
{
value = "" + value //convert value to string
var whole = "" + Math.round(value * Math.pow(10, 2));

var decPoint = whole.length - 2;
if(decPoint != 0)
{
result = whole.substring(0, decPoint);
result += ".";
result += whole.substring(decPoint, whole.length);
}
else
{
result = whole;
}
return result;
}

marvcollier
07-26-2002, 11:50 PM
Thanks

whammy
07-27-2002, 12:40 AM
Hmm... I needed this yesterday! Perhaps I can implement a modified version of this code tonight and use it Monday, if the client requests it. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum