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,537
  • 36
  • 105
  • 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
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 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
  • 47,184
  • 49
  • 157
  • 202
3
Console.WriteLine(System.Math.Round(0.69999980926513672d, 1));

-- edit

wow, you blink and there are 5 other answers!

Noon Silk
  • 54,084
  • 6
  • 88
  • 105
1

Use the Math.Round method:

double i = 0.69999980926513672;
double result = Math.Round(i, 2);
Ahmad Mageed
  • 94,561
  • 19
  • 163
  • 174