Truncate float numbers with PHP

Truncate float numbers with PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Truncate Float Numbers in PHP Without Rounding

Handling floating-point arithmetic in PHP is a classic source of frustration, particularly when you need to truncate a number to a specific decimal place without rounding up based on the next digit. Standard functions like round(), number_format(), and sprintf() are designed for rounding, not truncation. They will transform 45.8976 into 45.90 or 0.0185 into 0.02. If your application logic requires strict truncation—where 45.8976 must become 45.89 and 0.0185 must remain 0.01—you need a different approach than simply formatting output strings.

The core mathematical operation here is shifting the decimal point, flooring the integer result, and shifting it back. The challenge lies in floating-point precision; relying solely on standard math operators can sometimes introduce unexpected artifacts depending on how the language handles binary representation of decimals.

One proposed solution involves a complex conditional check using bccomp. This function compares two numbers as strings to ensure high precision. The logic presented suggests checking if the number is effectively zero (comparing against 0 with a scale of 10) and only applying a rounding adjustment if it isn't. Specifically, this snippet subtracts half of the target place value before rounding:

function truncNumber( $number, $prec = 2 )
{
    return bccomp( $number, 0, 10 ) == 0 ? $number : round( $number - pow( 0.1, bcadd(   $prec, 1 ) ) * 5, $prec );
}

While bccomp is robust for financial calculations, the logic here attempts to hack around the rounding behavior of round() by mathematically subtracting an offset before invoking it. This adds computational overhead and obscures the intent. It also relies on bcadd and pow with arbitrary precision strings, which might be overkill if standard floats suffice for your scale, but introduces potential performance costs in high-frequency loops.

A second solution offers a much more direct mathematical approach:

function truncNumber($number, $prec = 2 )
{
    return sprintf( "%.".$prec."f", floor( $number*pow( 10, $prec ) )/pow( 10, $prec ) );
}

This method is generally preferred for several reasons. First, it adheres to the standard algorithm for truncation: multiply by 10^precision, apply floor() to drop the fractional part representing the next digit, and divide by 10^precision to restore the scale. Second, by wrapping the final result in sprintf(), you ensure the output is formatted as a string with exactly the requested number of decimal places. Without sprintf(), simply dividing might return a float like 45.89 which, when printed or used in further math, could revert to its original imprecise binary representation (e.g., 45.890000000000001).

The trade-off with the sprintf() approach is that it returns a string. If your downstream logic requires a float for mathematical operations, you must explicitly cast the result back using (float). However, this is usually a negligible cost compared to the complexity of the first solution. The second solution avoids the need for BCMath extensions unless the input numbers are so large that standard floats lose precision immediately, which is rare in typical web applications.

When implementing this in a larger codebase or within a Laravel application, you might consider creating a helper method in your AppServiceProvider to keep these utilities centralized. If you are dealing with currency or critical financial data where even the smallest floating-point error is unacceptable, ensure you test edge cases like negative numbers and zero carefully. The floor() function behaves differently for negative numbers (e.g., floor(-45.89) is -46), so this logic holds true regardless of sign, which is a distinct advantage over simple multiplication/division without the floor step.

Ultimately, the second solution provides a clearer, more performant path to truncating floats in PHP. It leverages standard language features to achieve the desired mathematical result without relying on obscure arithmetic hacks or expensive arbitrary-precision libraries unless absolutely necessary for your specific data scale.

Our network