Skip to content
Advertisement

convert large values of string to number laravel. (int) gives wrong value

When I do:

$var = (int)('5000');
dd($var);  //outputs 5000
$query = Mymodel::find($id);
dd($query->loan_amount); //outputs "1,040,000.00"

Also:

$var = (int)($query->loan_amount);
dd($var);  //outputs 1

And finally:

$query = Mymodel::find($id);
dd($query->loan_amount); //outputs the picture below

enter image description here

May I know why? And how do I fix the large numbers?

Advertisement

Answer

The Model Collection has 104000 But output shows 1,040,000.00. So you might have used Accessor / Mutator.

You can try this to convert,

// remove `,` then convert.
$loan_amount = (int) (str_ireplace(',', '', $query->loan_amount));
dd($loan_amount);

Another way

Standard way to format model attributes in laravel is accessors & mutators, Please check it out.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement