3

Possible Duplicate:
Round a double to 2 significant figures after decimal point

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?

Community
  • 1
  • 1
happysmile
  • 7,179
  • 36
  • 101
  • 181

5 Answers5

13

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.

Community
  • 1
  • 1
paxdiablo
  • 814,905
  • 225
  • 1,535
  • 1,899
  • 1
    Leveraging your rich portfolio of answers +1 – Eric Schoonover Sep 08 '09 at 06:50
  • 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 Sep 08 '09 at 06:59
3

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)
Eric Schoonover
  • 45,692
  • 46
  • 149
  • 200
3
Console.WriteLine(System.Math.Round(0.69999980926513672d, 1));

-- edit

wow, you blink and there are 5 other answers!

Noon Silk
  • 52,938
  • 6
  • 85
  • 103
1

Use the Math.Round method:

double i = 0.69999980926513672;
double result = Math.Round(i, 2);
Ahmad Mageed
  • 91,579
  • 18
  • 159
  • 169