Reputation: 509
I'm having trouble with a tiny code in php:
$price = 135;
$price_sale = number_format($price * 0.75,2,',','');
//*returns 101,25 *//
$count_products = 3;
$new_price = number_format($price_sale * $count_products,2,',','');
//* returns 303,00 and not 303,75 *//
How can I fix this problem?
Regards,
Frank
Upvotes: 1
Views: 3611
Reputation: 54649
use:
$new_price = number_format($price * 0.75 * $count_products,2,',','');
as $price_sale
is a string
and won't probably have the value you're calculating with, after type casting. (See: http://php.net/manual/de/language.types.type-juggling.php)
Upvotes: 2
Reputation: 449425
Never do number_format
on numbers you want to do calculations with.
101,25
is not a valid number in PHP.
Work with the raw values until the number is output. Then, do a number_format()
.
Upvotes: 5
Reputation: 798646
Keep numbers as numbers. Don't format until the output stage.
Upvotes: 8