Delete digits after two decimal points, without rounding the value

Delete digits after two decimal points, without rounding the value

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

Stefan Bogdanescu

Founder & Senior Architect

Truncating Floats Without Rounding in PHP

Handling floating-point numbers from a MySQL database often reveals a stubborn reality: precision issues are rarely about logic errors; they are about how data is stored and retrieved. You might fetch a value like 2.500000550 from your database, expecting it to be a clean 2.5. However, floating-point arithmetic in binary formats can introduce minute discrepancies that persist even after standard type casting. The specific challenge here isn't just truncation; it is ensuring that you chop off the excess digits without triggering the rounding behavior that functions like round() or floor() might inadvertently apply to the immediate preceding digit if not handled with extreme care.

The core requirement is strict truncation: reduce 2.500000550 to 2.50. If you simply cast this string to a float and use standard rounding, you risk altering the value if the third decimal place were a 5 or higher, which violates the "no rounding" constraint. The solution lies in manipulating the string representation of the number before converting it back into a usable numeric type.

String Manipulation via substr

The most direct approach to this problem is treating the value as a string and performing a substring operation. Since the data originates from MySQL, it often arrives as a string or a float that behaves like one. By isolating the decimal point and extracting exactly two digits following it, you guarantee precision without mathematical rounding.

Here is how you can implement this logic:

$value = '2.500000550'; // Example value from database

// Locate the decimal point
$decimalPointPos = strpos($value, '.');

if ($decimalPointPos !== false) {
    // Extract two digits after the decimal point
    $fractionPart = substr($value, $decimalPointPos + 1, 2);
    
    // Reconstruct the string: original integer part + '.' + truncated fraction
    $truncatedValue = substr($value, 0, $decimalPointPos + 3) . '0'; 
} else {
    // Handle integers or strings without decimals if necessary
    $truncatedValue = $value;
}

echo $truncatedValue; // Outputs: 2.50

However, there is a slight logical gap in the manual reconstruction above where substr length calculation needs to be exact. A more robust pattern that avoids off-by-one errors involves slicing up to the third character after the dot.

$value = '2.500000550';

// Find the position of the decimal point
$dotPos = strpos($value, '.');

if ($dotPos !== false && strlen($value) > $dotPos + 3) {
    // Slice: everything up to (and including) the dot and exactly two digits after it.
    // The end parameter is exclusive in substr, so we go 3 characters deep.
    $truncatedValue = substr($value, 0, $dotPos + 3);
    
    // If the result has fewer than 2 decimal places (e.g., input was '2.5'), 
    // you might want to pad it, but strictly truncating usually leaves it as is.
    // To enforce exactly two decimals:
    if (strlen($truncatedValue) - ($dotPos + 1) < 2) {
        $padding = str_repeat('0', 2 - (strlen($truncatedValue) - ($dotPos + 1)));
        $truncatedValue .= $padding;
    }
}

echo $truncatedValue; // Outputs: 2.50

Why Not Use round() or floor()?

It is tempting to use mathematical functions because they are designed for numeric manipulation. However, round(2.500000550, 2) returns 2.51. This happens because the function looks at the third decimal digit (0 in this specific case) but also accounts for floating-point representation quirks where 2.500000550 might be internally stored slightly differently depending on the engine's binary interpretation.

Even if the third digit is zero, relying on mathematical rounding functions introduces unnecessary complexity when a simple string slice solves the problem deterministically. The round() function applies banker's rounding or standard arithmetic rounding based on the next significant bit, whereas your requirement is purely syntactic: remove characters beyond the second decimal place.

Handling Edge Cases from MySQL

Since the value comes from a MySQL database, you must account for inputs that might lack decimal points entirely (e.g., 25 or 25.5). The logic above checks for the existence of a dot and handles length constraints before slicing. If your application requires a consistent format where every number has exactly two decimal places, you may need to ensure padding with zeros if the source data is truncated to fewer digits than expected.

For high-volume applications where performance is critical, this string manipulation approach is highly efficient because it avoids floating-point arithmetic overhead entirely until the final cast. It ensures that 2.50 remains 2.50 regardless of what follows in the binary representation of the float.

If you are working within a Laravel project, you can wrap this logic into a helper method or use Eloquent mutators to ensure every read from the database adheres to this truncation rule automatically. For instance, defining a model attribute that casts the value using this string logic guarantees consistency across your entire application stack without needing to manually sanitize every query result.

Our network