Yasser
Yasser

Reputation: 325

Fraction approximation to the nearest

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#?

Upvotes: 2

Views: 4797

Answers (3)

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361692

Use Math.Round().

double d1 = Math.Round(1.2324); //d1 is 1
double d2 = Math.Round(1.898);  //d2 is 2

Upvotes: 3

Oosterman
Oosterman

Reputation: 384

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.

Upvotes: 0

Related Questions