Skip to content

Core\Math::floor

Rounds a number down to the next whole number. 4.9 becomes 4.0, and a number that is already whole stays the same.

For a negative number, down means away from zero: -4.1 becomes -5.0. A number between -1.0 and 0.0 becomes -1.0. The result is always a float. NaN (a value that means "not a number") and the infinities stay the same.

Use it when only a complete unit counts: a full ten euros, a full hour, a full box. Core\Math::ceil rounds up, Core\Math::truncate rounds toward zero, and Core\Math::round rounds to the nearest number.

The examples below show a few numbers rounded down, then negative numbers, then how a shop gives one loyalty point for every full ten euros a customer spends.

Parameters

  • $nfloatThe number to round down.

Return value

Returns float. A whole number, as a float. A $n between -1.0 and 0.0 gives -1.0. NaN and the infinities stay the same.

Examples

A few numbers rounded down

<?nvs
// Shows a few numbers rounded down with `Core\Math::floor`.
// The fraction is removed, even when it is close to 1. This prints 4.
echo Core\Math::floor(4.9), "\n";
// A whole number stays the same. This prints 4.
echo Core\Math::floor(4.0), "\n";
// A number below 1 becomes 0. This prints 0.
echo Core\Math::floor(0.75), "\n";
Output
4
4
0

Negative numbers

<?nvs
// Shows how `Core\Math::floor` rounds a negative number.
// Down means away from zero for a negative number. This prints -5.
echo Core\Math::floor(-4.1), "\n";
// A number between -1 and 0 becomes -1. This prints -1.
echo Core\Math::floor(-0.5), "\n";
// `Core\Math::truncate` rounds toward zero instead. This prints -4.
echo Core\Math::truncate(-4.1), "\n";
Output
-5
-1
-4

Loyalty points

<?nvs
// Shows how a shop gives one loyalty point for every full ten euros a customer spends.
class Loyalty
{
// Only a full ten euros gives a point. The rest of the amount gives nothing.
public static function points(float $spent): float
{
return Core\Math::floor($spent / 10.0);
}
}
// 47.50 euros is four full tens. This prints 4.
echo Loyalty::points(47.5), "\n";
// Exactly 30 euros is three full tens. This prints 3.
echo Loyalty::points(30.0), "\n";
// 9.99 euros is not a full ten. This prints 0.
echo Loyalty::points(9.99), "\n";
Output
4
3
0