Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All
C#
  I am trying to calculate  using RoundUp(). I am using the following code.

  Double per =(Double)Convert.ToDecimal((1.92* 100) / 2.2);
  Double p = RoundUp(per, 2);

 It is giving me a value of p =87.28
 while value of per is 87.27272722727277

What is the reason for this? Surely it should be returning a value of 87.27 right?

Please enlighten me. Thanks.
Posted
Comments
CPallini 28-Nov-13 8:40am    
What is 'RoundUp()'? Do you defined it?
Karthik_Mahalingam 28-Nov-13 8:44am    
post the code for RoundUP method
Sergey Alexandrovich Kryukov 28-Nov-13 11:27am    
Why? Rounding is rarely needed and can be dangerous. Most people who ask about rounding in fact needs certain rounded string formatting for numeric values...
What is "RoundUp"?
—SA

Hi ErBhati,

I'd guess your RoundUp function was created to "always round up" - seems logical for me, and seems to work as expected, no? Anyway if you want not "round up always" just use built-in
Math.Round() function...

Kind regards Johannes
 
Share this answer
 
Comments
ErBhati 29-Nov-13 0:23am    
thanks its work for me
If the purpose of the RoundUp() function is ... to round values UP ... then it looks like it's working correctly?

If you want your values simply rounded, either Up or Down, then have a look at the Round function:
C#
Math.Round(per, 2)
 
Share this answer
 
Comments
ErBhati 29-Nov-13 0:23am    
thanks
Have a read of this, in particular Jon Skeets answer

http://stackoverflow.com/questions/977796/why-does-math-round2-5-return-2-instead-of-3-in-c[^]

and this MSDN

http://msdn.microsoft.com/en-us/library/wyk4d9cy.aspx[^]

They are saying that

Quote:

Return Value
Type: System.Double
The integer nearest a. If the fractional component of a is halfway between two integers, one of which is even and the other odd, then the even number is returned. Note that this method returns a Double instead of an integral type.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Nov-13 11:37am    
This is not the case OP shows. It should be 87.27 in all possible rounding methods, but OP got 87.28. But we have no information on how OP got it. Who knows what "RoundUp" is? I would not bother to answer at all...

Rounding is not so trivial thing, odd/even rule is also can cause shift in distribution.

I always question if the rounding is needed at all. Usually, when inquirers ask for rounding, they don't really need it.

—SA
Simon_Whale 28-Nov-13 11:57am    
Actually a good read is on stackoverflow (http://stackoverflow.com/questions/1584314/conversion-of-a-decimal-to-double-number-in-c-sharp-results-in-a-difference), there is a rounding issue converting a decimal into a double.

"For a conversion from decimal to float or double, the decimal value is rounded to the nearest double or float value. While this conversion may lose precision, it never causes an exception to be thrown."

so it would appear that the problem isn't the rounding by the type conversion from decimal to double.
Sergey Alexandrovich Kryukov 28-Nov-13 13:33pm    
Exactly.
—SA
Adam Zgagacz 28-Nov-13 17:15pm    
Do you have source code of your RoundUp method? By analyzing it we can tell why it is behaving this way.
Thanks to all for giving your valueable time
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900