Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

.net

math

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

like image 490
Anonymous User Avatar asked Aug 01 '08 00:08

Anonymous User


People also ask

Is Math trunc and Math floor the same?

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.

Is trunc and floor the same?

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.

What is the difference between trunc and floor in SQL?

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.

What is the difference between Math Ceil () and Math floor ()?

Math. ceil() - rounds the number to a higher value. Math. floor() - rounds the number to a lower value.


1 Answers

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!

like image 161
Chris Jester-Young Avatar answered Oct 11 '22 01:10

Chris Jester-Young