Reputation: 3065
I am using Accessor for format my number value. Problem start when i try to calculate the formatted value because number_format() function return a string value. Is there any alternative way to format the number value and calculate the value also.
class Order extends Model
{
protected $fillable = ['order_quantity'];
public function getOrderQuantityAttribute($value)
{
return number_format($value);
}
}
Error shown when i try to calculate like,
$order->order_quantity * 100;
//$order->order_quantity value is 10,000
Upvotes: 1
Views: 598
Reputation: 687
just replace ',' with '' :
str_replace(',','',$order->order_quantity) *10
Upvotes: 0
Reputation: 210
may be this will help you. using str_replace
remove comma from string.
$newOrderQuantity = str_replace(',', '', $order->order_quantity); // converting to "10000"
so on, you can use (int)$newOrderQuantity
for calculation.
Upvotes: 2