Click here to Skip to main content
13,048,805 members (71,045 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
I want the rounded off value from the rounded value like :
If I rounded 5.51 --> to 6.00
I want the value which rounded(Here .49 how i get this value?),
and
If I rounded 5.48 --> to 5.00
I want the value which rounded(Here .48 how i get this value?)

Please Help me out..

Thanks In advance....
Posted 2-Mar-13 20:23pm
Updated 1-Aug-16 1:38am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

First of all, you should look at the problem from a different side and think why do you need rounding. I don't believe you really need it. It is very rarely needed. (One rare exclusion I known is generation of pseudo-random numbers; and I doubt that you are developing something like a new random number generator. :-))

Much more likely that you simply want to show a rounded value on screen, and this is something very different. In this case, you should think about formatting, but not rounding itself. Please see:
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx[^].

By the way, even if you find a correct way to solve the problem using rounding, think again. It's much safer avoid doing it and focusing on formatting only. You don't want rounding to sneak into your calculations even by accident.

Good luck,
—SA
  Permalink  
Comments
Abhinav S 14-Apr-13 2:00am
   
My 5.
   
Thank you, Abhinav.
—SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Follow the answer Round Off decimal values in C#[^].
Quote:
Look at Math.Round(decimal)[^] or the overload which takes a MidpointRounding argument.[^]

Of course, you'll need to parse and format the value to get it from/to text. If this is input entered by the user, you should probably use decimal.TryParse, using the return value to determine whether or not the input was valid.

string text = "19500.55";
decimal value;
if (decimal.TryParse(text, out value))
{
    value = Math.Round(value);
    text = value.ToString();
    // Do something with the new text value
}
else
{
    // Tell the user their input is invalid
}

Comments
Jayanta Chatterjee 3-Mar-13 2:55am
   
Sir, I know the round function and it's works..
I solved it my self...
thanks for helping me... :-)
Tadit Dash 3-Mar-13 3:14am
   
Ok... Welcome...

So, please accept this answer. I can see that you have also posted one answer.

Accept this answer as well, so that it will be helpful for others and you will be awarded with some points.

Thanks,
Tadit
Espen Harlinn 14-Apr-13 16:35pm
   
5'ed!
Tadit Dash 15-Apr-13 4:32am
   
Thanks a lot Espen Harlinn... :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Hi you can do like this:

double a = 5.50;
string[] str= a.ToString().Split('.');
double num1= Convert.ToDouble("0."+str[1]);
double res;
if (num1 < 0.51)
{
     res= Math.Floor(a);
}
else {
    res = Math.Round(a); 
}
  Permalink  
v3
Comments
King Fisher 1-Aug-16 7:52am
   
oi shobi,how are you :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

we can calculate it simply like
lets assume,

 

 
double originalVal = 51.25;  // value before round off
 
double roudedVal = Math.Round(originalVal, MidpointRounding.AwayFromZero); // rounded value
 
double GetVal = 0;  // to know rounded value
 
GetVal -= originalVal - roudedVal;
 
GetVal = Math.Round(GetVal, 2);  // to again roundup value with just 2 decimal precision, if it show like 0.47999909 to show up as 0.48
 
MessageBox.Show(GetVal.ToString());
 
// output will be -25
 
  Permalink  
v4
Comments
pwasser 1-Aug-16 7:49am
   
Nonsense reply to a 3 year old question which already has sane answers.
Member 12473157 1-Aug-16 13:46pm
   
sorry if you have not understand my old answer.. check my updated answer..
this trick worked perfectly for me and
even if this is older question but many new people are looking for it so, i shared it with others. try it and then reply if it has any problem..

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 1 Aug 2016
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100