Skip to content

Weird Result from Decimal Calculation #15746

@zelosnhp

Description

@zelosnhp

Found out that the result was different when I execute below simple calculation.

$total_percentage = 33.4 + 33.3 + 33.3;
if ($total_percentage < 100) {
echo "Total less than " . $total_percentage;
} else {
if ($total_percentage > 100) {
echo "Total more than " . $total_percentage;
} else {
echo "Total is " . $total_percentage;
}
}

The result is weird: Total less than 100

But if I simply change the sequence to:
$total_percentage = 33.3 + 33.3 + 33.4;
Or value of first 2 to:
$total_percentage = 33.5 + 33.2 + 33.3;

Then both result is correct as expected: Total is 100

And if want to remain the sequence and i add with number_format:
if (number_format($total_percentage, 1) < 100) {

The result is also correct: Total is 100

This happen to all version of PHP from 4.0.6 to 8.3.10 and only if the value is 33.4 + 33.3 + 33.3;

What could be the possibility cause of this?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions