What is the difference between Math.Floor()
and Math.Truncate()
in .NET?
Math. trunc rounds down a number to an integer towards 0 while Math. floor rounds down a number to an integer towards -Infinity . As illustrated with the following number line, the direction will be the same for a positive number while for a negative number, the directions will be the opposite.
Definition of floor R function: The floor function rounds a numeric input down to the next lower integer. Definition of trunc R function: The trunc function truncates (i.e. cuts off) the decimal places of a numeric input.
The FLOOR function is the counterpoint to CEIL, returning the next integer equal to or lower than the input value, or “rounding down.” The last function TRUNC, cuts off the fractional part of a number, leaving only the integer portion. Thus always returning a value of equal or smaller absolute value.
Math. ceil() - rounds the number to a higher value. Math. floor() - rounds the number to a lower value.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With