2012-05-31

Round double values to the nearest value in C#

If you want to round to the nearest upper perfect number, use the following code.

double a = Math.Ceiling(551425.451078772);

If you want to round to the nearest lower perfect number, use the following code.

double a = Math.Floor(551425.451078772);

Math.Ceiling method will always return the smallest integer which is greater than the provided double value. Math.Floor method will always return the largest integer which is lower than the provided double value. But both function will return the integer value as 'double' type. So you have to assign it to a double type variable.

No comments: