This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET Framework
.NET 1.1+

.NET Math Library

The .NET framework includes a class named "Math", which provides a number of standard mathematical functions, using static methods, and mathematical values, using simple constants. This article describes all of the Math class members.


IEEERemainder is another method that calculates a remainder following a division. The result can be different to the remainder from DivRem as the calculation is different. For DivRem, the remainder is calculated using a modulus operation. With IEEERemainder, the remainder is generated using a formula defined in the IEEE 754-1985 standard for floating-point operations. The standard states that the remainder following the division x / y should be calculated as x - (round(x / y) * y). This means that it is possible to produce a negative remainder when dividing two positive numbers.

The method works with doubles only, although other types can be cast to and from double as required. This also allows the possibility for calculating the remainder following division by zero where the result is NaN.

double remainder;
remainder = Math.IEEERemainder(9, 3);       // remainder = 0.0
remainder = Math.IEEERemainder(10, 3);      // remainder = 1.0
remainder = Math.IEEERemainder(11, 3);      // remainder = -1.0
remainder = Math.IEEERemainder(10.5, 3);    // remainder = -1.5
remainder = Math.IEEERemainder(10, 0);      // remainder = NaN


The rounding functions allow a floating-point value to be rounded to an integer or to a specified number of decimal places. Various rounding rules are available, depending upon the method used and the .NET framework version.


Ceiling rounds a number to produce an integer. The value is always rounded upwards to find the first integer that is equal to or greater than the input parameter. When using the .NET framework version 2.0 you can find the ceiling value for doubles or decimals. In earlier .NET versions only doubles can be processed.

double ceiling;
ceiling = Math.Ceiling(0.1);    // ceiling = 1.0
ceiling = Math.Ceiling(0.5);    // ceiling = 1.0
ceiling = Math.Ceiling(1.5);    // ceiling = 2.0
ceiling = Math.Ceiling(2.0);    // ceiling = 2.0
ceiling = Math.Ceiling(-0.1);   // ceiling = 0.0


Floor is similar to Ceiling as it rounds doubles and decimals to produce integer values. The difference is that the result is the first integer that is equal to or less than the value being processed.

double floor;
floor = Math.Floor(0.1);        // floor = 0.0
floor = Math.Floor(0.5);        // floor = 0.0
floor = Math.Floor(1.5);        // floor = 1.0
floor = Math.Floor(2.0);        // floor = 2.0
floor = Math.Floor(-0.1);       // floor = -1.0


Truncate is a third method that rounds decimals or doubles to integers. The rounding is performed by removing the fraction portion of the argument. This means that the number is always rounded towards zero; negative values are rounded upwards and positive values downward. The method is not available in the .NET framework version 1.1.

double truncated;
truncated = Math.Truncate(0.1);     // truncated = 0.0
truncated = Math.Truncate(0.5);     // truncated = 0.0
truncated = Math.Truncate(1.5);     // truncated = 1.0
truncated = Math.Truncate(2.0);     // truncated = 2.0
truncated = Math.Truncate(-0.1);    // truncated = 0.0


Round provides rounding for doubles and decimals, returning the same type as the value being rounded. When using the .NET framework version 1.1, four overloaded versions of the method are available. The simplest two overloads have a single parameter that accepts a value to be rounded to an integer. The rounding rules are different to that of Floor, Ceiling or Truncate. In most cases the return value is the result of rounding the input value to the nearest integer. However, if the value being rounded is exactly half way between two integers, for example 1.5, the result is the nearest even number. This is sometimes known as "Banker's Rounding".

double rounded;
rounded = Math.Round(0.1);      // rounded = 0.0
rounded = Math.Round(0.5);      // rounded = 0.0
rounded = Math.Round(1.5);      // rounded = 2.0
rounded = Math.Round(2.0);      // rounded = 2.0
rounded = Math.Round(-0.1);     // rounded = 0.0

Specifying a Number of Decimal Places

You can add a second, integer parameter to the Round method to specify the number of decimal places in the result. The rounding still uses banker's rounding when the value is exactly half way between two possible answers, generating a result with an even final digit.

double rounded;
rounded = Math.Round(0.41, 0);      // rounded = 0.0
rounded = Math.Round(0.35, 1);      // rounded = 0.4
rounded = Math.Round(1.25, 1);      // rounded = 1.2
rounded = Math.Round(2.0003, 2);    // rounded = 2.0
rounded = Math.Round(-0.155, 2);    // rounded = -0.16

Controlling Midpoint Rounding

In version 2.0 of the .NET framework the Round method was enhanced to allow you to control how rounding is carried out when a value is midway between two possible values. Each of the four previous overloads can be given an additional argument, which can be one of the MidpointRounding enumeration's constants. If the ToEven option is chosen, the behaviour is the same as if the argument were omitted; the banker's rounding rule applies. Alternatively, you can elect to use AwayFromZero rounding. This chooses the value furthest from zero when two possible rounding values are available. For positive numbers rounding is upwards at the midpoint, for negative numbers rounding is downward.

double rounded;
rounded = Math.Round(0.5, MidpointRounding.ToEven);             // rounded = 0.0
rounded = Math.Round(0.5, MidpointRounding.AwayFromZero);       // rounded = 1.0
rounded = Math.Round(-1.25, 1, MidpointRounding.ToEven);        // rounded = -1.2
rounded = Math.Round(-1.25, 1, MidpointRounding.AwayFromZero);  // rounded = -1.3
7 October 2011