I'm having trouble with a tiny code in php:
$price = 135;
$price_sale = n开发者_开发问答umber_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
Keep numbers as numbers. Don't format until the output stage.
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()
.
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)
精彩评论