views:

410

answers:

7

I'm rounding up my decimal values to integer values ex:

I have 5 numbers whose % sum should end up being 100% but i get 101% my values are

11.11111, 80.55555, 5.55555, 2.77777

and on rounding them i get

11, 81, 6, 3 => 101

In above for example 5.555556 rounds up to 6 (correct value) but is screwing up my total for percentage which is going above 100 when i round and add numbers. How can i force or change round function to give desired value? I'm using

round(100 * (x1/sum) )

My output on webpage is like: (i want to display % rounded up to int)

field value %
x      8    11
y      58   81

Is this not possible?

Note: I'm using asp round function - updated

+2  A: 

It looks to me like 5.556 should round to 6, actually. Maybe your school taught math differently?

Conrad Meyer
nope i know it should round to 6 :DI was basing my answer on end result. I have 5 numbers whose % sum should end up being 100% but i get 101% my values are 11.11111, 80.55555, 5.55555, 2.77777 and on rounding them i get 11, 81, 6, 3 => 101
rs
@Rahuls: You could write your own rounding function which makes .5 go down and .6 go up. But this will still get you errors where your sum could be 99. Is there a reason you need to round before the addition?
Yuriy Faktorovich
yes i want values without decimal and that is why i was rounding them. i have number and its percentage displayed in a table with total at end.
rs
+2  A: 

Why do you think 5.55556 should round to 5? Rounding algorithms round up OR down, depending on which integer is closest. Clearly 6 is closer to 5.55556 than 5 is (it's great than 5.5,) so 6 is the expected and correct answer.

Take a look at the floor and ceil functions for alternative implementations of always rounding down and always rounding up respectively.

-Oisin

x0n
Updated question, i didn't mention my issue correctly
rs
unless the function somehow knows about all the numbers involved, you're asking the impossible.
x0n
+2  A: 

Maybe you mistyped, but 5.55556 should actually round UP to 6 (anything .5 and above rounds up, and anything below .5 rounds down). If you want better control, then I would look into the floor and ciel functions. floor always rounds down and ciel always rounds up.

Gabriel McAdams
I don't think classic ASP has the floor and ceiling functions, so you would have to write your own implementation.
jaywon
updated question, my previous question was misleading.
rs
It seems that what you're really trying to do is 2 separate things. One - you want each value to be rounded. Two - you want the sum of all values to be rounded. I would round the sum of all pre-rounded numbers to get your total.
Gabriel McAdams
A: 

try doing:

round(100m * (x1/sum) )

which should force both factors to be treated as decimals instead of ints. Also, make sure (x1/sum) doesn't have a greater number in the denominator.

Jim Schubert
A: 

You should add all your values then do the rounding.

By nature, rounding gives you a little more or a little less. So if you add values that all are a little more you will get over 100.

Joe
yes i understand i have to add and do rounding, but my requirement is such that i have to round them up individually. Is round not good solution here? SHould i do it some other way
rs
Without knowing the exact problem/requirement it's hard to just guess at a solution
Joe
A: 

VB (and VBScript / ASP 3) uses banker's rounding (or "round to the nearest even") so:

1.5 rounds to 2
2.5 rounds to 2
3.5 rounds to 3
4.5 rounds to 4
... etc.

I would suggest doing as Jim Schubert suggested and writing your own rounding function to round properly.

This page has some good info about rounding across all vb-esque languages:

http://support.microsoft.com/kb/196652

Luu
+1  A: 

If you do individual roundings, and than want the total to be some exact number too, the last individual number must always be the end-result minus all previous rounded numbers.

This is the case in ASP but also the case in Excel f.i. AFAIK this is the only way to achiveve this.

Edelcom