3

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

patricus
  • 59,488
  • 15
  • 143
  • 145
Eday Gonzalez
  • 310
  • 1
  • 2
  • 13

2 Answers2

3

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)
patricus
  • 59,488
  • 15
  • 143
  • 145
0

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.

Tim Sheehan
  • 3,994
  • 1
  • 15
  • 18