Ad

Convert Large Values Of String To Number Laravel. (int) Gives Wrong Value

- 1 answer

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?

Ad

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.

Ad
source: stackoverflow.com
Ad