c# – How to round off a number?

Question:

There is a number 1.08 . How do I make it round to 2? Those. if the number is more than 1 at least in thousandths, then we increase it to 2.

Answer:

Rounding to the nearest larger integer:

double d = Math.Ceiling(1.08); // => 2

Rounding to the nearest smaller integer:

double d = Math.Floor(1.08); // => 1

Discarding the fractional part:

double d = Math.Truncate(1.08); // => 1

It may seem like Floor and Truncate identical, but this is not the case for negative numbers:

double d = Math.Floor(-1.08); // => -2

whereas

double d = Math.Truncate(-1.08); // => -1
Scroll to Top