2

I have double number and i want to approximate it always to the nearest .

for eg: 1.2324 -> 1 1.898 -> 2

how can i do this with C#?

Amittai Shapira
  • 3,749
  • 1
  • 30
  • 54
Yasser
  • 315
  • 2
  • 10
  • 15
  • [Using Round?](http://msdn.microsoft.com/en-us/library/system.math.round(v=vs.71).aspx) – bzlm Sep 18 '11 at 09:47
  • See http://stackoverflow.com/questions/14/whats-the-difference-between-math-floor-and-math-truncate-in-net/580252#580252 - it tells you everything you need to know about the various rounding and truncating methods. – paxdiablo Sep 18 '11 at 09:53

3 Answers3

3

Use Math.Round().

double d1 = Math.Round(1.2324); //d1 is 1
double d2 = Math.Round(1.898);  //d2 is 2
Nawaz
  • 353,942
  • 115
  • 666
  • 851
0

Try Math.Round http://msdn.microsoft.com/en-us/library/aa340225(v=vs.71).aspx

Radu
  • 923
  • 2
  • 9
  • 21
0

Math.Round(double) does the trick. If you want to use a variable number of decimals behind the decimal point you can use Math.Round(double, int)

The API describing this is here.

Oosterman
  • 383
  • 2
  • 11