PHP How do I round down to two decimal places?

PHP How do I round down to two decimal places?

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

Stefan Bogdanescu

Founder & Senior Architect

Why round() and number_format() Fail for Flooring Decimals

When you need to truncate a decimal value in PHP rather than round it, the built-in functions often behave counter-intuitively. A common requirement is flooring a number to two decimal places. For example, taking 49.955 and converting it strictly to 49.95, discarding any excess without rounding up.

If you attempt this using number_format(49.955, 2), PHP returns 49.96. This happens because number_format performs standard rounding (round half up). Similarly, the standard round() function will also round 49.955 up to 49.96. These functions are designed for financial calculations where nearest-value rounding is required, not truncation.

Attempting to solve this by extracting characters with substr is a common but flawed approach. While number_format(49.955, 2) might output the string "49.96", using substr((string)$number, 0, -1) could work for large numbers like 49.955. However, it fails completely for smaller values or those with fewer decimal places. If your input is 7.950 and you simply chop the last character, you lose precision or alter the value incorrectly depending on how PHP represents that float internally. Furthermore, string manipulation bypasses type safety; converting a float to a string can introduce floating-point representation errors that make truncation unreliable for financial data.

The correct approach relies on arithmetic scaling rather than string manipulation. To floor a number to two decimal places, you must multiply the value by 100, floor it to an integer (removing any fractional part), and then divide by 100. This method respects the numeric nature of the variable while enforcing the truncation logic.

Here is the implementation pattern:

function floorToTwoDecimals(float $number): float {
    return (int)($number * 100) / 100;
}

// Usage
echo floorToTwoDecimals(49.955); // Outputs: 49.95
echo floorToTwoDecimals(7.950);  // Outputs: 7.95
echo floorToTwoDecimals(7.1);    // Outputs: 7.1

In this example, multiplying 49.955 by 100 yields 4995.5. Casting this to an integer (int) truncates the .5, resulting in 4995. Dividing back by 100.0 restores the decimal point, yielding 49.95.

This logic handles the edge case of smaller numbers naturally. When you pass 7.950, multiplying by 100 gives 795.0. The integer cast keeps it as 795, and dividing by 100 results in 7.95. Unlike string slicing, this method does not care about the total length of the number or the position of the decimal point; it simply scales the magnitude down to an integer domain, applies the floor operation there, and scales back up.

Be aware that the return type is technically a float. If you require a string representation for database storage or display, you may need to format the output afterwards using number_format or printf. However, keep in mind that if you use number_format on the result of this function, it will still round for display purposes unless you format with a specific precision logic. For instance, storing 49.95 as a string requires care to ensure it doesn't become 49.9 or 49.95000001 due to floating-point quirks.

For high-precision financial applications where even this arithmetic approach might feel risky due to IEEE 754 floating-point limitations, consider using the BC Math functions (bcround, bcmul) with specific scale settings or working entirely in integers (cents). These methods avoid the inherent imprecision of binary floats but require more verbose syntax.

If you are building an application where consistent rounding behavior across different environments is critical, ensure your team understands the distinction between rounding and truncating. Misinterpreting these requirements often leads to financial discrepancies that standard library functions do not prevent on their own. For deeper insights into handling currency in PHP, resources like LaravelCompany offer guidance on best practices for financial calculations within the framework ecosystem.

Our network