Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want to round down,round up the decimal number.
For example,12.9879-->12.97--round down
12.9879--->12.99--round up
Posted
Comments
CPallini 16-May-13 8:42am    
12.9879-->12.97
This is not round-down. The correct round-down here is: 12.9879-->12.98
lukeer 16-May-13 8:46am    
I understand why you want to round 12.9879 up with two decimals to 12.99.
But I cannot think of anything sane that would result in rounding the same number down even below 12.98.
Please use the "Improve question" link and provide some more insight on the problem.

I doubt you really need rounding per se. It is almost never needed. Chances are, you only need to present the value on screen in a rounded form. And this is something different, as you don't do rounding directly and only use proper string formatting. Please see:
http://msdn.microsoft.com/en-us/library/fzeeb5cd.aspx[^],
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx[^].

Note that is is also safer then rounding: you guarantee that rounding does not sneak into calculations and does not cause precision loss.

—SA
 
Share this answer
 
Firstly your rounding logic in your question appears to be wrong.... 12.9879 should round down to 12.98 if you want it to round down.

However, it took me all of about 2 seconds with google to find this link:

http://stackoverflow.com/questions/2632753/rounding-values-up-or-down-in-c-sharp[^]
 
Share this answer
 
public static decimal Round(
decimal d,
int decimals
)

where d - A decimal number to be rounded, and decimals - The number of decimal places in the return value.
 
Share this answer
 
v2
Comments
CPallini 16-May-13 9:13am    
That is 'round', i.e. not 'round-up' neither 'round-down'.
Alexander Dymshyts 16-May-13 9:32am    
Thnx for fixing )

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