floor will do as you ask.

floor(45.8976 * 100) / 100;

You won't find a more direct function for this, since it's a kind of odd thing to ask. Normally you'll round mathematically correct. Out of curiosity - What do you need this for?


this is my solution:

/**
 * @example truncate(-1.49999, 2); // returns -1.49
 * @example truncate(.49999, 3); // returns 0.499
 * @param float $val
 * @param int f
 * @return float
 */
function truncate($val, $f="0")
{
    if(($p = strpos($val, '.')) !== false) {
        $val = floatval(substr($val, 0, $p + 1 + $f));
    }
    return $val;
}

function truncate($value)
{
    if ($value < 0)
    {
        return ceil((string)($value * 100)) / 100;
    }
    else
    {
        return floor((string)($value * 100)) / 100;
    }
}

Why does PHP not handle mathamatic formulas the way we would expect, e.g. floor(10.04 * 100) = 1003 when we all know it should be 1004. This issue is not just in PHP but can be found in all langauges, depending on the relative error in the langauge used. PHP uses IEEE 754 double precision format which has a relative error of about 1.11e-16. (resource)

The real issue is that the floor function casts the float value into an int value, e.g. (int)(10.04 * 100) = 1003 as we see in the floor function earlier. (resource)

So to overcome this issue we can cast the float to a string, a string can represent any value accurately, then the floor function will cast the string to an int accurately.