Click here to Skip to main content
15,906,333 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
If the value is 0.0009785 means,i want to get the value like this 0.98
Posted
Comments
phil.o 27-Sep-13 0:39am    
Non sense. The value you will get would be 0.00 with 2 decimal points rounding. Did you make a typo?

I believe you don't really need to round any numeric values. This is rarely needed, very rarely. Who wants to loose precision in calculations? Most likely, you only want to present a number in a form of string, to show the value on screen or somewhere else.

This is called "formatting". Even though such operation is internally also based on rounding, you never round any numeric values in your code, so you avoid a risk of accidental loss of accuracy. And to format the numerical values to string, you just need to use one of its ToString methods with parameters setting format, precision, culture and/or other formatting detail. For example, for double, please see:
http://msdn.microsoft.com/en-us/library/system.double.tostring.aspx[^],
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx[^].

—SA
 
Share this answer
 
Watch for Math.Round(Double, Integer) function.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Sep-13 0:46am    
Formally, this is correct, but I don't believe OP really needs rounding.
This is a subtle moment. Please see my answer.
—SA
double m = Math.Round(0.55000000000000004,2);
 
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