jeremyj11
jeremyj11

Reputation: 704

PHP 7.3 Laravel database auto number rounding error

Strange error that I can't seem to find the cause of: my decimal numbers are being automatically rounded when saved to database - only when I use PHP 7.3. The problem doesn't occur when using PHP 7.1 (I'm switching between the 2 versions using whm's multi php manager)

10.1.39-MariaDB Laravel 5.5, using eloquent, tested by doing:

$model->num=1.23;
$model->save();

Works fine when using php 7.1, the value 1.23 I saved correctly to the database, however when using php 7.3 the value is rounded down to 1.00.

I can see that the actual SQL sent by laravel's query builder is correct in both cases: UPDATE x SET num=1.23 WHERE id=x so somewhere inbetween the query builder and actual execution the value is being rounded.

I also wrote a pure php test without using Laravel using mysqli_query("UPDATE ...") and this works correctly in both php versions, so I'm thinking that the problem must be in some Laravel database package incompatibility perhaps.

Don't really know where to go from here - any advise anyone?

Thanks in advance.

UPDATE

On php 7.3 I get the value saved to db correctly if I cast to string first:

$model->num= (string)1.23;
$model->save();

I don't really want to do this though as there will be many places I'd have to do this... still investigating the cause

Upvotes: 1

Views: 1041

Answers (1)

Karthik SWOT
Karthik SWOT

Reputation: 1217

Use number_format($value, 2) It will be definitely works.

Detail format is: number_format($value, 2, '.', '');

Upvotes: 1

Related Questions