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

Decimal loses precision because of internal cast to float #45454

Closed
Sjord opened this issue Dec 29, 2022 · 1 comment · Fixed by #45456
Closed

Decimal loses precision because of internal cast to float #45454

Sjord opened this issue Dec 29, 2022 · 1 comment · Fixed by #45456

Comments

@Sjord
Copy link
Contributor

Sjord commented Dec 29, 2022

  • Laravel Version: illuminate/database 9.45
  • PHP Version: PHP 8.2.0
  • Database Driver & Version: none

Description:

When specifying a field to be cast as decimal, it is still internally converted to float by the call to number_format. This loses precision.

Steps To Reproduce:

<?php

require('vendor/autoload.php');

use Illuminate\Database\Eloquent\Model;

final class Member extends Model {
    public $timestamps = false;
    protected $casts = [
        'amount' => 'decimal:20',
    ];
}

$member = new Member();
$member->amount = '0.1';
var_dump($member->amount);

Outputs:

string(22) "0.10000000000000000555"

Expected:

string(22) "0.10000000000000000000"
@timacdonald
Copy link
Member

timacdonald commented Dec 30, 2022

I've opened a PR to start addressing this. We can continue the conversation in the PR instead.

See #45456

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

Successfully merging a pull request may close this issue.

2 participants