...

View Full Version : Resolved Rounding numbers

longman
06-16-2009, 07:37 AM
Hi All,

Is there a simple way of doing the following? I am working with percentages by using a basic operator ((number / 365)*100). If the resultant value is below 50% (regardless of how many decimal places there are), the number needs to round down to the nearest whole percent. If the value is above 50%, it should be rounded up to the nearst whole percent.

I have not been able to find a way. Ultimately, I am looking at including this within a function that does the above opertion to convert it to percentage so I can apply other rules to the result, and not having it as a separate function unless there is no other way. I am using IE6.

Any help would be appreciated. I have looked into the round method but couldn't find how to tell it do do what I want.

Cheers,

A

Philip M
06-16-2009, 08:32 AM
Try this:-

<script type = "text/javascript">
var num = 323;
var a = num/365*100;
if (a < 50) {
var result = parseInt(Math.floor(a));
}
else {
var result = parseInt(Math.ceil(a));
}