tags:

views:

240

answers:

5

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?

+1  A: 

Use the Math.Round method:

double i = 0.69999980926513672;
double result = Math.Round(i, 2);
Ahmad Mageed
+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
+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
Leveraging your rich portfolio of answers +1
spoon16
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
+3  A: 
Console.WriteLine(System.Math.Round(0.69999980926513672d, 1));

-- edit

wow, you blink and there are 5 other answers!

Noon Silk
This is one of the most commonly asked "math" questions on Stack Overflow.
Greg Hewgill