Skip to content

Error in round() and number_format()Β #17722

@joaopalopes24

Description

@joaopalopes24

Description

I have this code:

<?php

addAmount(50.00, 'update', 112.99);

function addAmount(float $addedValue, ?string $type, float $baseAmount): string
{
    if ($addedValue) {
        $baseAmount += $type == 'fixed' ? $addedValue : $baseAmount / 100 * $addedValue;
    }


    var_dump(
        $baseAmount,
        round($baseAmount, 2),
        number_format(169.485, 2, '.', ''),
        number_format($baseAmount, 2, '.', ''),
    );

    return number_format($baseAmount, 2, '.', '');
}

In PHP 8.3, If I run this code, the result is:

float(169.48499999999999)
float(169.49)
string(6) "169.49"
string(6) "169.49"

But if I run this same code in PHP 8.4, the result is:

float(169.48499999999999)
float(169.48)
string(6) "169.49"
string(6) "169.48"

PHP Version

8.4.3

Operating System

Ubuntu 22.04

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