How to Use the PHP Function Floor When Working With Numbers and Fractions
Often when working with numbers and in particular fractions, you want to be able to round the value down. For this you need to know how to use the php function floor. This article will give you a quick overview of what this function does, along with some easy to follow coding examples.
Floor finds the next lowest integer value and returns it as a float. This is the opposite of ceil, which rounds up to the nearest integer. Using these math functions along with number_format can make working with numbers and fractions in PHP much easier.
If a positive floating-point number is given as argument to floor(), it will convert it to an integer (as opposed to typecasting it) and return the result. This is not true for negative numbers, however. if you give a negative float to floor(), it will actually round the number down. So for example, if the float is -3.5, it will become -4 (not -3).
This behavior is caused by the fact that floor() and round() are both rounding functions that work on decimal values. The difference between these two is that while rounding a number up to the nearest integer like ceil does, floor works the other way around by rounding down to an integer.