I have a value var i = 0.69999980926513672
. I need to round this value to 0.7
is there a built-in method that will do this?
views:
240answers:
5
+1
A:
Use the Math.Round method:
double i = 0.69999980926513672;
double result = Math.Round(i, 2);
Ahmad Mageed
2009-09-08 06:42:04
+3
A:
You are looking for the Math.Round
method.
//first param is number to round
//second param is the accuracy to use in the rounding (number of decimal places)
Math.Round(i, 2)
spoon16
2009-09-08 06:42:18
+13
A:
Use one of:
System.Math.Round (i, 1, MidpointRounding.ToEven);
System.Math.Round (i, 1, MidpointRounding.AwayFromZero);
The difference is how it handles numbers that are equidistant to the rounding point (e.g., 0.65 in your case could either go to 0.7 or 0.6).
Here is a answer I gave to another question which holds much more information.
paxdiablo
2009-09-08 06:42:44
Leveraging your rich portfolio of answers +1
spoon16
2009-09-08 06:50:16
As my wife says when I'm in full flight, conversationally speaking, the less people are forced to listen to me, the happier they seem to become :-) So it's probably best to keep the answers short and reference my more voluminous essays with links.
paxdiablo
2009-09-08 06:59:58
+3
A:
Console.WriteLine(System.Math.Round(0.69999980926513672d, 1));
-- edit
wow, you blink and there are 5 other answers!
Noon Silk
2009-09-08 06:43:18
This is one of the most commonly asked "math" questions on Stack Overflow.
Greg Hewgill
2009-09-08 06:48:10