View Full Version : Rounding numbers after calculations

marvcollier

07-26-2002, 09: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, 10: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, 10:50 PM

Thanks

whammy

07-26-2002, 11:40 PM

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. :)

Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.