Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a requirement where decimal with .5 and less should be rounded down and decimal with .6 and above should be rounded up. Let me explain you with an example.

13.5 --> 13.0 is the expected value
13.6 --> 14.0 is the expected value

Now should i have to code it or is there any built in functionality in c#.

Please confirm

[EDIT]

What is meant is

13.4 --> 13 is the expected value
13.5 --> 13 is the expected value
13.6 --> 14 is the expected value

Please confirm if i have to code or is there any in built functionality
Posted
Updated 27-Apr-11 20:20pm
v2

C#
double num = 5.5;
     int intpart = (int)num;
     double decpart = num - intpart;
     if (decpart > .5)
     {
         intpart = intpart + 1;
     }




here is your answer

happy coding ;)
 
Share this answer
 
 
Share this answer
 
Comments
Rajesh Anuhya 28-Apr-11 3:06am    
Not a Helpful answer for OP
-2
m@dhu 28-Apr-11 4:28am    
Well the op wants to round the value. Math.Round(13.4) gives 13.
or am I missing something? :doh:
Rajesh Anuhya 28-Apr-11 4:34am    
Your link for Math.Round function, but OP already know the Round function, his requirement is different. and normal Math.Round(13.5) will return 14 not 13
m@dhu 28-Apr-11 4:58am    
OP could use the function with an if condition for his requirement something similar you posted. Thanks for the comment.
Math.Round(13.5) will return 14 not 13
Thats a typo. Corrected.
Rajesh Anuhya 28-Apr-11 5:00am    
solution is given, not for banker's rounding
Try this logic..

MIDL
double val = Convert.ToDouble(textBox1.Text);
MessageBox.Show(Math.Round((val % .5) == 0 ? (val - 0.1) : val).ToString());
 
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