Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I am using C# windows application where I have to calculate decimal number (condition is if number is 3.5 or above up to 4 then it should be 4 or if number is less then 3.5 up to 3 then it should be 3). It is applicable in all decimal number how can I make that please help.

Thanks to All
Posted

You may use Math.Round[^]. One of its overloads allows you to specify both the precision and how to round the midpoint value.
 
Share this answer
 
Comments
Reiss 2-Aug-11 5:28am    
Most complete answer out of the group
Sergey Alexandrovich Kryukov 3-Aug-11 4:50am    
I don't think rounding is really needed -- it is used quite rarely; most likely, only the rounded string presentation is only needed, which is quite different thing. Please see my solution.
--SA
CPallini 3-Aug-11 4:54am    
If the OP asks me a beer I may propose a (more healtly) glass of water or, well, I may give her/him the f#@*in' beer. :-)
BobJanova 3-Aug-11 7:11am    
On CP they often ask for beer when what they need is water (or even a tap). But yes, your answer is complete and correct regarding the actual question asked. I suspect SA is right though so I gave him a 5 as well.
CPallini 3-Aug-11 7:38am    
I agree with you: probably most of them don't even know what actually need. However, since there are a lot of folks here able to give good advices, I usually provide, as alternative, a direct answer.
BTW Thanks.
I doubt you really need rounding: it is needed very rarely. I suspect you only need a rounded presentation of the number to be shown on the screen, which is a completely different thing. You really should not round any values before you present them.

Use string.Format with proper precision specifiers.
See:
http://msdn.microsoft.com/en-us/library/system.string.format.aspx[^],
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx[^].

—SA
 
Share this answer
 
What you are looking for is the Math.Round method.

C#
int num = Math.Round(myNumber);
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Aug-11 4:51am    
I don't think rounding is really needed -- it is used quite rarely; most likely, only the rounded string presentation is only needed, which is quite different thing. Please see my solution.
--SA
See[^] Math.Round.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Aug-11 4:51am    
I don't think rounding is really needed -- it is used quite rarely; most likely, only the rounded string presentation is only needed, which is quite different thing. Please see my solution.
--SA
C#
double test = 3.4;
int result = Math.Round(test,1,MidpointRounding.AwayFromZero);

Do you mean MidpointRounding.AwayFromZero?

you can refer MSDN abort AwayFromZero:
http://msdn.microsoft.com/en-us/library/system.midpointrounding.aspx[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 3-Aug-11 4:51am    
I don't think rounding is really needed -- it is used quite rarely; most likely, only the rounded string presentation is only needed, which is quite different thing. Please see my solution.
--SA

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