Eday Gonzalez
Eday Gonzalez

Reputation: 310

Laravel Eloquent truncates the data type double

Laravel truncate value of type double.

The value 3.539363636363637 is showed as 3.5393636363636

I do this so:

$e=Enrollment::find(173);
dd($e->value);
// show 3.5393636363636

In phpmyadmin the value is correct 3.539363636363637

Upvotes: 3

Views: 1428

Answers (2)

patricus
patricus

Reputation: 62228

Laravel is not truncating your data; PHP is. The default precision for floating point numbers in PHP is 14 significant digits. If you were to raise your precision up to 16 digits, your value would print fine.

PhpMyAdmin is showing the "correct" value because it treats it as a string; it never actually converts it to a floating point number.

ini_set('precision', 16);

$e=Enrollment::find(173);
dd($e->value);
// would show 3.539363636363637 (all 16 significant digits)

Upvotes: 3

Tim Sheehan
Tim Sheehan

Reputation: 4014

Try changing from double to decimal, I can remember someone having some issues with double types and rounding at some point so it might be worth a shot.

Upvotes: 0

Related Questions