Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i need to round my double value after 2 decimal point.
that is 18.0051 to 18.00
but when i tried using math.round it results 18.01
please someone help me

What I have tried:

Math.Round(18.0051, 2, MidpointRounding.AwayFromZero)
Math.Round(18.0051, 2,MidpointRounding.ToEven)
Posted
Updated 4-Nov-16 0:34am
Comments
Mehdi Gholam 1-Nov-16 7:10am    
The definition of rounding a number is to the nearest number, and .0051 is nearer to .01 that .0000
Philippe Mori 1-Nov-16 9:17am    
Your example is poorly selected (as Math.Floor(18.0051) would also give 18.00) and your wording is incorrect (truncating vs rounding).
#realJSOP 1-Nov-16 9:40am    
If your value is 18.1151, what value do you ultimately want to see? According to the way you stated your question, you're expecting "18.11". Is that correct?
#realJSOP 1-Nov-16 11:32am    
Solution 3 is the answer you want.

Well as other people point out, it is rounding it. If you are looking to just lop off the digits after first two fractional ones you can do this:

double y = Math.Floor(value*100d)/100d;

Bear in mind that doubles can cause subtle accuracy problems though. You might want to consider using decimals instead.
 
Share this answer
 
Comments
#realJSOP 1-Nov-16 11:16am    
Better answer than mine. BTW, you can probably resolve the math-induced accuracy problems by rounding the result:

value = Math.Round(Math.Floor(value*100d)/100d, 2);

Also, if the value you're starting with is negative, you have to call Math.Ceiling instead.

value = Math.Round(((value >= 0d) ? Math.Floor(value * 100d) : Math.Ceiling(value * 100d)) / 100d, 2);
The value 18.0051 will ALWAYS round to 18.01. To do what you want, you could use string manipulation:

C#
double value = 18.0051;
string str = value.ToString();
int pos = Math.Min(str.Length, str.IndexOf(".") + 3);
if (pos > 0)
{
	str = str.Substring(0, pos);
}
value = Convert.ToDouble(str);


You could put that code into an extension method so it's always available.

C#
public static double Truncate(this double value, int places)
{
    double result = value;
    string str = value.ToString();
    int pos = Math.Min(str.Length, str.IndexOf(".") + places + 1);
    if (pos > 0)
    {
        str = str.Substring(0, pos);
    }
    result = Convert.ToDouble(str);
    return result;
}


Using the new extension method would reduce your outward facing code to a single line:

C#
double value = 18.0051;
value = value.Truncate(2);
 
Share this answer
 
v3
Comments
Philippe Mori 1-Nov-16 8:48am    
This code is too complex compared to say solution 3... and it would not be robust as it would fails if decimal separator is not a dot or for some arguments (less that one decimal place, big or small number...).
#realJSOP 1-Nov-16 9:11am    
According to his example and stated requirements, the code I provided is fine. If he needs culture-specific code, I'm sure he'll ask here before trying to figure it out on his own.
Philippe Mori 1-Nov-16 10:21am    
Well, the problem with that is that it can lead to subtle bugs. Say that the input number is 1.23e-10, then the output would be 1.23 instead of 0.00. Also, in such code, you have to ensure that the invariant culture is used. That kind of code leads to bugs like web site works in English but not in French (or vice-versa). And often, a programmer would only works in one language and maybe with good data... so that kind of problems might not be detected before shipping if you don't have a QA department.
Below url has answer for your question

Round double in two decimal places in C#? - Stack Overflow[^]
 
Share this answer
 
Comments
mufeed k 1-Nov-16 7:33am    
i mean my double value is 18.0051 and i want to round this to 18.00 (after 2 decimal point)
KARTHICKEYANRAJU 1-Nov-16 7:37am    
Did you mean Ceiling or flooring? Check out below one

https://msdn.microsoft.com/en-us/library/zx4t0t48(v=vs.110).aspx

First Floor the value and convert to float
#realJSOP 1-Nov-16 8:06am    
That's not what he wants (if I'm reading his question correctly). If his value is "18.12345", he'll get "18.00" if he does it your way. He wants "18.12".
sorry guys, actually it was a misunderstanding between truncate and round. i got the solution and thank you all for the help
 
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