Removing all decimals in PHP

Removing all decimals in PHP

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

Stefan Bogdanescu

Founder & Senior Architect

Extracting Integers from Decimals in PHP

When you pull a value like 252.587254564 directly from a database, it often arrives as a float or a string representing a decimal number. If your application logic requires the whole integer part—specifically 252 without the fractional remainder—you need to perform an integer conversion or truncation operation. The choice of method depends heavily on how that data was stored and whether you are working with floating-point arithmetic or text strings.

If the value exists in your database as a numeric type (like DECIMAL, FLOAT, or DOUBLE), it is almost certainly stored as a float internally. In PHP, the standard way to extract the integer portion of a number is using the (int) cast operator. This performs truncation toward zero, effectively chopping off everything after the decimal point without rounding.

$value = 252.587254564;
$integerPart = (int) $value;

// Result: 252

This approach is efficient and straightforward for most scenarios where the fractional part is unwanted regardless of its magnitude. However, you must be aware of how PHP handles floating-point numbers. Because binary floating-point arithmetic can introduce precision errors, a value that looks like 252.0 might internally be stored as something slightly larger or smaller depending on previous calculations. While casting (int) generally works well for positive numbers, it can behave unexpectedly if the number is extremely large or negative, though truncation toward zero remains consistent in those cases.

If you are using a framework like Laravel, the syntax remains identical because PHP's core casting operators work outside of the framework layer. You might see this inside a controller receiving data from a form or an API:

public function store(Request $request)
{
    $price = (int) $request->input('amount');
    // Logic continues...
}

There is another approach often confused with casting: the floor() function. While (int) simply cuts off the decimals, floor() rounds down to the nearest integer. For positive numbers like 252.9, both yield 252. However, for negative numbers, they diverge. If your database contains values like -252.1, casting (int) results in -252 (truncating toward zero), whereas floor() results in -253 (rounding down). If you strictly want to remove decimals without rounding logic, stick with the cast operator.

If your data is stored as a string in the database, such as '252.587254564', casting it directly to an integer will also work in PHP because of its loose type conversion rules:

$stringValue = '252.587254564';
$integerPart = (int) $stringValue; // Result: 252

However, relying on implicit casting for strings can be risky if the input format changes or contains non-numeric characters. If you need stricter validation before conversion, consider using floatval() first to normalize the data, then cast it. This ensures that any string like 'abc' is handled explicitly rather than resulting in a warning or unexpected behavior later in your code.

$safeValue = (int) floatval($stringValue);

In summary, if your goal is simply to discard the fractional component of a number, (int) is the most direct and performant solution for both float and string inputs. Reserve floor() or round() only if your specific business logic requires rounding down or to the nearest integer respectively. Always verify the data type coming from your source to ensure consistent behavior across different environments.

Our network