Search code examples
laravelnumber-formattingphp-7.2

A non well formed numeric value encountered After Formatted by number_format() function


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

Solution

  • 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.