Peter
Peter

Reputation: 1916

Php ignores decimals while adding two numbers

When adding two number it ignores the decimals, in database the product_price and product_shipping_cost data type is decimal(10,2)

$product_price = 272.70; $product_price = 189.00; $product_shipping_cost 14.00;

The total should be 475.70 but i get 475.7 as the output how do i fix this i have tried adding (float) but still same

foreach($mycart as $row_checker){
    $CKItemSubtotal += (float)(($row_checker->product_discount>0) ? $row_checker->product_price * ((100-$row_checker->product_discount) / 100) * $row_checker->cart_qty : $row_checker->product_price * $row_checker->cart_qty) + $row_checker->product_shipping_cost;
}

Please can anyone help me

Upvotes: 0

Views: 52

Answers (2)

Gildas
Gildas

Reputation: 1158

You may also format number using printf. In your case: printf('%.2f', 475.7);, but that's up to you.

Upvotes: 1

hexYeah
hexYeah

Reputation: 1150

number_format($number, 2)

If two parameters are given, number will be formatted with decimals decimals with a dot (".") in front, and a comma (",") between every group of thousands.

Upvotes: 1

Related Questions