Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP 8.1 Implicit conversion from float to int loses precision #2549

Closed
levu42 opened this issue Feb 17, 2022 · 1 comment · Fixed by #2551
Closed

PHP 8.1 Implicit conversion from float to int loses precision #2549

levu42 opened this issue Feb 17, 2022 · 1 comment · Fixed by #2551
Labels
Milestone

Comments

@levu42
Copy link

levu42 commented Feb 17, 2022

Hello,

I encountered an issue with the following code:

echo Carbon::parse('2018-06-15')->isoFormat('Do');

Carbon version: 2.56.0

PHP version: 8.1.2

I expected to get:

15th

But I actually get:

"Error: Implicit conversion from float 1.5 to int loses precision"

The issue is in src/Carbon/Lang/en.php on line 75:

    'ordinal' => function ($number) {
        $lastDigit = $number % 10;

        return $number.(
            (~~($number % 100 / 10) === 1) ? 'th' : (
                ($lastDigit === 1) ? 'st' : (
                    ($lastDigit === 2) ? 'nd' : (
                        ($lastDigit === 3) ? 'rd' : 'th'
                    )
                )
            )
        );
    },

Specifically in:

~~($number % 100 / 10) === 1

Thanks!

levu42 added a commit to levu42/Carbon that referenced this issue Feb 17, 2022
Compare float to float, not float to int
@kylekatarnls
Copy link
Collaborator

Thanks for the report.

For the record, I can't reproduce as an "Error" I can only get "Deprecated: Implicit conversion from float 1.5 to int loses precision".

Normally this will only happen with E_DEPRECATED on and will become an error only in PHP 9.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants