How to get whole and decimal part of a number?

How to get whole and decimal part of a number?

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

Stefan Bogdanescu

Founder & Senior Architect

Extracting Whole and Decimal Parts in PHP

Separating a floating-point number into its integer component and fractional remainder is a common requirement when validating currency, handling scientific data, or implementing custom rounding logic. If you are working with a value like 1.25 and need to isolate the "1" from the .25, you must distinguish between simple type casting and actual mathematical division.

The most straightforward approach in PHP is using the floor() function to extract the whole part. This function rounds a number down to the nearest integer, effectively stripping away everything after the decimal point.

$number = 1.25;
$wholePart = (int) floor($number); // Results in 1

Casting this result directly to an integer ensures you are not left with floating-point artifacts. If you attempt to use (int)$number directly without floor(), PHP will handle positive numbers similarly, but relying on implicit casting can be risky if the input is negative. For example, casting -1.25 to an integer yields -1, whereas truncation towards zero is often what developers expect for simple "whole part" extraction in mathematical contexts. Using floor() guarantees behavior consistent with the definition of the whole part regardless of sign.

To retrieve the decimal part itself, you should subtract the extracted whole part from the original number. However, there is a critical pitfall here: floating-point precision errors. In many languages and architectures, 1.25 - 1 might not result in exactly 0.25 due to how IEEE 754 standard floats are stored. While 0.25 usually has an exact binary representation, operations like 1.1 + 2.2 failing to equal 3.3 are common knowledge among engineers.

$decimalPart = $number - $wholePart; 
// Mathematically 0.25, but internally it might be 0.24999999999999997

If your goal is to check if the decimal part is .0, .25, .5, or .75, you are likely dealing with currency or specific rational intervals. Comparing the raw $decimalPart directly using == can lead to false negatives because of those precision errors. Instead, round the calculated difference to a fixed number of significant digits before comparison.

$cleanDecimal = round($number - $wholePart, 2);

if ($cleanDecimal == 0) {
    echo "Whole number";
} elseif ($cleanDecimal == 0.25) {
    echo ".25 fraction";
} elseif ($cleanDecimal == 0.5) {
    echo ".5 fraction";
} elseif ($cleanDecimal == 0.75) {
    echo ".75 fraction";
}

A more robust alternative for financial applications is to work with integers representing the smallest currency unit (cents or pennies) rather than floating-point decimals from the start. This eliminates precision issues entirely. You would multiply your input by 100, cast it to an integer, and then use modulo arithmetic to find the remainder.

$cents = round($number * 100);
$wholeCents = floor($cents / 100);
$decimalCents = $cents % 100; // Gives 25 for 1.25

This approach makes the logic for checking .0, .25, .5, and .75 trivial: you simply check if $decimalCents is 0, 25, 50, or 75. Since floating-point numbers are notoriously unreliable for exact equality checks, converting to an integer scale before performing the modulo operation is the industry standard for financial calculations.

If you need further guidance on handling monetary values or building robust math utilities in your next Laravel project, you can explore resources at https://laravelcompany.com which often covers these edge cases in depth.

Our network