Difference between Math.Floor() and Math.Truncate()
Asked Answered
P

14

507

What is the difference between Math.Floor() and Math.Truncate() in .NET?

Paunch answered 1/8, 2008 at 0:59 Comment(1)
e.g. Math.Floor(5.4) = 5 Math.Truncate(5.4) = 5Guertin
F
583

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!

Foreside answered 1/8, 2008 at 12:26 Comment(4)
@Chris, I suggest you fix your description of Round, there's two ways to round (AwayFromZero and ToEven) and it doesn't round to the nearest integer since it can do fractional rounding as well.Bragdon
So just a short add on to the original question - what is the difference between Math.Truncate and just casting a decimal or double to int? wouldn't it also just round towards zero?Jahdol
When does (int)myDouble differ from (int)Math.Truncate(myDouble)?Ptosis
What is (int) equal to in Math class?Flinders
B
443

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
Bragdon answered 24/2, 2009 at 2:39 Comment(4)
Pax, I think you've got a mistake with: Round(AwayFromZero) -3 -2 1 2 3 Math.Round(-1.2, MidpointRounding.AwayFromZero) == -1 Math.Round(0.3, MidpointRounding.AwayFromZero)==0.0 etc..Spratt
Thanks, @dtroy, I've never had a need to use that mode and, while I documented it correctly if the text, I totally got the examples wrong. Hopefully that's fixed now.Bragdon
Sorry to comment on such an old question but I have to ask: How can you round "ToEven" to two decimal places? Surely odd and even apply only to integers?De
@Richiban, think of even as a property of the final digit in the rounded number, not as meaning the entire number must be a multiple of two. By the way, sorry it took so long to get back to you, hope you weren't just sitting around waiting for my response :-)Bragdon
D
73

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

while

Math.Floor(3.4)     = 3
Math.Truncate(3.4)  = 3
Daydream answered 19/7, 2011 at 3:56 Comment(0)
U
60

Math.floor sliiiide to the left...
Math.ceil sliiiide to the right...
Math.truncate criiiiss crooooss (floor/ceil always towards 0)
Math.round cha cha, real smooth... (go to closest side)

Let's go to work! (⌐□_□)

To the left... Math.floor
Take it back now y'all... --
Two hops this time... -=2

Everybody clap your hands ✋✋

How low can you go? Can you go down low? All the way to the floor?

if (this == "wrong")
    return "i don't wanna be right";

Math.truncate(x) is also the same as int(x).
by removing a positive or negative fraction, you're always heading towards 0.

Urolith answered 11/2, 2018 at 15:3 Comment(0)
C
48

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
Cookie answered 5/8, 2008 at 11:1 Comment(0)
C
30

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
Calamander answered 2/1, 2015 at 13:9 Comment(0)
S
29

Try this, Examples:

Math.Floor() vs Math.Truncate()

Math.Floor(2.56) = 2
Math.Floor(3.22) = 3
Math.Floor(-2.56) = -3
Math.Floor(-3.26) = -4

Math.Truncate(2.56) = 2
Math.Truncate(2.00) = 2
Math.Truncate(1.20) = 1
Math.Truncate(-3.26) = -3
Math.Truncate(-3.96) = -3

Also Math.Round()

   Math.Round(1.6) = 2
   Math.Round(-8.56) = -9
   Math.Round(8.16) = 8
   Math.Round(8.50) = 8
   Math.Round(8.51) = 9

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

Shorter answered 12/2, 2016 at 9:12 Comment(0)
S
26

Math.Floor() rounds "toward negative infinity" in compliance to IEEE Standard 754 section 4.

Math.Truncate() rounds " to the nearest integer towards zero."

Squeal answered 7/6, 2012 at 18:15 Comment(0)
T
15

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.

Tadich answered 19/9, 2013 at 11:44 Comment(1)
The OP asked about the difference between Floor() and Truncate(), not Floor() and Round().Terrapin
E
5

Math.floor() will always round down ie., it returns LESSER integer. While round() will return the NEAREST integer

math.floor()

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

math.truncate()

Calculates the integral part of a number.

Edgerton answered 17/7, 2018 at 7:14 Comment(0)
S
3

Math.Floor() :

It gives the largest integer less than or equal to the given number.

    Math.Floor(3.45) =3
    Math.Floor(-3.45) =-4

Math.Truncate():

It removes the decimal places of the number and replace with zero

Math.Truncate(3.45)=3
 Math.Truncate(-3.45)=-3

Also from above examples we can see that floor and truncate are same for positive numbers.

Sibell answered 24/6, 2020 at 8:43 Comment(0)
A
1

Truncate drops the decimal point.

Attaboy answered 22/3, 2020 at 19:17 Comment(0)
T
1

Going by the Mathematical Definition of Floor, that is, "Greatest integer less than or equal to a number", This is completely unambiguous, whereas, Truncate just removes the fractional part, which is equivalent to round towards 0.

Theadora answered 28/11, 2020 at 16:49 Comment(0)
L
1

In .NET, both Math.Floor() and Math.Truncate() are mathematical functions used to round down a decimal number to the nearest integer. However, there is a subtle difference between the two:

1: Math.Floor():

Math.Floor() always rounds down the decimal number to the nearest integer towards negative infinity. It returns the largest integer less than or equal to the specified number. For positive numbers, Math.Floor() rounds down to the immediate lower integer. For negative numbers, Math.Floor() rounds down to the next lower integer (further away from zero).

Example:

Math.Floor(3.8) will result in 3.

Math.Floor(-3.8) will result in -4.

2: Math.Truncate():

Math.Truncate() also rounds down the decimal number to the nearest integer, but it does so towards zero. It removes the fractional part without performing rounding. For positive numbers, Math.Truncate() behaves the same way as Math.Floor(). For negative numbers, Math.Truncate() rounds towards zero, so the result is closer to zero.

Example:

Math.Truncate(3.8) will result in 3.

Math.Truncate(-3.8) will result in -3.

Lamdin answered 4/4 at 10:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.