Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a calculation function in that function having value 21.1049

After using Math.Round(value,2)

i want the out put value = 21.11

How can i do this, please suggest me the solution?

What I have tried:

I have tried this
double a = 21.1049;

// but is gives 21.10 out output
double b = Math.Round(a,2);
b COnvert.ToDouble(a.ToStrin("0.00");
b COnvert.ToDouble(a.ToStrin("0.##");
b = Math.Round( a ,2,MidpointRounding.AwayFromZero);
b = Math.Round( a ,2,MidpointRounding.ToEven);
b = a.ToString("N2");

all are giving me the output 21.10
Posted
Updated 29-May-16 23:02pm
Comments
Sergey Alexandrovich Kryukov 30-May-16 3:32am    
Rounding through string formatting is really bad, never do it. More importantly, I don't think you need rounding pe se.
—SA

Avoid using Convert unless it is really needed, which is a rare case. In your case, you don't need it at all. Instead, use Parse or TryParse methods for the primitive types you need to use. What you do is really parsing, not "conversion". For example, for double, please see:
Double.Parse Method (System)[^],
Double.TryParse Method (System)[^].

Now, I hardly can believe that you really need rounding. Again, you rarely need to use rounding directly. If you do, you mostly risk to create loss of precision, that's it. No intermediate results should be rounded. Rounding is only needed for output, say, when you need to show some number of screen. But in this case, this is not directly rounding, this is formatting:
Double.ToString Method (System)[^],
Standard Numeric Format Strings[^],
Custom Numeric Format Strings[^].

—SA
 
Share this answer
 
Thanks for your suggesions and answers i got the answer after lots of searching and trial and error , I got whatever i want, so i use the following solution.
Once again for than you guys for your help.

C#
public static Double myRound(Double Value, int places = 1000)
{
    Double myvalue = (Double)Value;
    if (places == 1000)
    {
        if (myvalue - (int)myvalue == 0.5)
        {
            myvalue = myvalue + 0.1;
            return (Double)Math.Round(myvalue);
        }
        return (Double)Math.Round(myvalue);
        places = myvalue.ToString().Substring(myvalue.ToString().IndexOf(".") + 1).Length - 1;
    } if ((myvalue * Math.Pow(10, places)) - (int)(myvalue * Math.Pow(10, places)) > 0.49)
    {
        myvalue = (myvalue * Math.Pow(10, places + 1)) + 1;
        myvalue = (myvalue / Math.Pow(10, places + 1));
    }
    return (Double)Math.Round(myvalue, places);
}
 
Share this answer
 
Hi,
I propose for your case to use:
b = Math.Ceiling(a*100.0)/100.0;

Best regards
 
Share this answer
 
Comments
SweetSSS 30-May-16 2:44am    
Thanks a lot. Its working !!!

But in the following case i want the 21.10 result can you please help me again.
double d = 21.1004;
double a = Math.Ceiling(d * 100) / 100;

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