mykoman
mykoman

Reputation: 1905

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?

Upvotes: 0

Views: 592

Answers (1)

BadPiggie
BadPiggie

Reputation: 6359

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.

Upvotes: 1

Related Questions