# Difference between Math.Floor() and Math.Truncate()

Follow these links for the MSDN descriptions of:

`Math.Floor`

, which rounds down towards negative infinity.`Math.Ceiling`

, which rounds up towards positive infinity.`Math.Truncate`

, which rounds up or down towards zero.`Math.Round`

, which rounds to the nearest integer or specified number of decimal places. You can specify the behavior if it’s exactly equidistant between two possibilities, such as rounding so that the final digit is even (“`Round(2.5,MidpointRounding.ToEven)`

” becoming 2) or so that it’s further away from zero (“`Round(2.5,MidpointRounding.AwayFromZero)`

” becoming 3).

The following diagram and table may help:

```
-3 -2 -1 0 1 2 3
+--|------+---------+----|----+--|------+----|----+-------|-+
a b c d e
a=-2.7 b=-0.5 c=0.3 d=1.5 e=2.8
====== ====== ===== ===== =====
Floor -3 -1 0 1 2
Ceiling -2 0 1 2 3
Truncate -2 0 0 1 2
Round (ToEven) -3 0 0 2 3
Round (AwayFromZero) -3 -1 0 2 3
```

Note that `Round`

is a lot more powerful than it seems, simply because it can round to a specific number of decimal places. All the others round to zero decimals always. For example:

```
n = 3.145;
a = System.Math.Round (n, 2, MidpointRounding.ToEven); // 3.14
b = System.Math.Round (n, 2, MidpointRounding.AwayFromZero); // 3.15
```

With the other functions, you have to use multiply/divide trickery to achieve the same effect:

```
c = System.Math.Truncate (n * 100) / 100; // 3.14
d = System.Math.Ceiling (n * 100) / 100; // 3.15
```

They are functionally equivalent with positive numbers. The difference is in how they handle negative numbers.

For example:

```
Math.Floor(2.5) = 2
Math.Truncate(2.5) = 2
Math.Floor(-2.5) = -3
Math.Truncate(-2.5) = -2
```

MSDN links:

– Math.Floor Method

– Math.Truncate Method

P.S. Beware of Math.Round it may not be what you expect.

To get the “standard” rounding result use:

```
float myFloat = 4.5;
Console.WriteLine( Math.Round(myFloat) ); // writes 4
Console.WriteLine( Math.Round(myFloat, 0, MidpointRounding.AwayFromZero) ) //writes 5
Console.WriteLine( myFloat.ToString("F0") ); // writes 5
```

`Math.Floor()`

rounds

“toward negative infinity” in compliance to IEEE Standard 754 section 4.

`Math.Truncate()`

rounds ” to the nearest integer towards zero.”

`Math.Floor`

rounds down, `Math.Ceiling`

rounds up, and `Math.Truncate`

rounds towards zero. Thus, `Math.Truncate`

is like `Math.Floor`

for positive numbers, and like `Math.Ceiling`

for negative numbers. Here’s the reference.

For completeness, `Math.Round`

rounds to the nearest integer. If the number is exactly midway between two integers, then it rounds towards the even one. Reference.

See also: Pax Diablo’s answer. Highly recommended!

Some examples:

```
Round(1.5) = 2
Round(2.5) = 2
Round(1.5, MidpointRounding.AwayFromZero) = 2
Round(2.5, MidpointRounding.AwayFromZero) = 3
Round(1.55, 1) = 1.6
Round(1.65, 1) = 1.6
Round(1.55, 1, MidpointRounding.AwayFromZero) = 1.6
Round(1.65, 1, MidpointRounding.AwayFromZero) = 1.7
Truncate(2.10) = 2
Truncate(2.00) = 2
Truncate(1.90) = 1
Truncate(1.80) = 1
```

`Math.Floor()`

rounds toward negative infinity

`Math.Truncate`

rounds up or down towards zero.

For example:

```
Math.Floor(-3.4) = -4
Math.Truncate(-3.4) = -3
```

`math.floor()`

Returns the largest integer less than or equal to the specified number.

MSDN system.math.floor

`math.truncate()`

Calculates the integral part of a number.

MSDN system.math.truncate

`Math.Floor()`

: Returns the largest integer less than or equal to the specified double-precision floating-point number.

`Math.Round()`

: Rounds a value to the nearest integer or to the specified number of fractional digits.