Reputation: 126
When I want to store decimal numbers in mysql decimal field formatted like**(8,4), but it's not stored completely. ie **13850 is stored as 13.0000. This is my code in controller (Laravel):
$getRate = DB::table('currency_rates')
->select('crt_id','buy_rate')->orderBy('created_at','desc')->first();
$rate = $getRate->buy_rate;
$c_price = ($unit_price*$rate);
$expense->c_price = number_format($c_price,4);
$expense->c_total = number_format($quantity*$c_price,4);
// dd(number_format($quantity*$c_price,4));
$expense->rate_id = $getRate->crt_id;
Your help really appreciated.
Upvotes: 1
Views: 513
Reputation: 14110
DECIMAL(8,4) means that the number can have up to 8 digits and 4 of them must be after the point, so your range is -9999.9999 to 9999.9999.
You have to allow more digits to store 13850.
UPDATE: Don't use number_format() before storing number to database, use it when displaying only.
Number_format uses comma as a thousands separator, but comma can't be stored in any numeric field in mysql.
number_format(13850, 4) returns string "13,850.0000" and that's why you get 13 stored in database.
Upvotes: 1