What is the difference between Math.Floor()
and Math.Truncate()
in .NET?
original title: ".net - Difference between Math.Floor() and Math.Truncate()"
What is the difference between Math.Floor()
and Math.Truncate()
in .NET?
Qual è la differenza tra Math.Floor () e Math.Truncate () in .NET?
Questo è il riepilogo dopo la traduzione, se è necessario visualizzare la traduzione completa, fare clic sull'icona "traduci"
Math.Floor
rounds down,Math.Ceiling
rounds up, andMath.Truncate
rounds towards zero. Thus,Math.Truncate
is likeMath.Floor
for positive numbers, and likeMath.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!
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:
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:With the other functions, you have to use multiply/divide trickery to achieve the same effect:
Math.Floor()
rounds toward negative infinityMath.Truncate
rounds up or down towards zero.For example:
while
Some examples:
They are functionally equivalent with positive numbers. The difference is in how they handle negative numbers.
For example:
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:
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()
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
In addition Math.Round()
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
?Math.truncate(x)
is also the same asint(x)
.by removing a positive or negative fraction, you're always heading towards 0.
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.Mat.floor()
will always round down ie., it returns LESSER integer. Whileround()
will return the NEAREST integer