开发者

number_format in a number_format doesn't work

开发者 https://www.devze.com 2023-03-06 14:20 出处:网络
I\'m having trouble with a tiny code in php: $price = 135; $price_sale = n开发者_开发问答umber_format($price * 0.75,2,\',\',\'\');

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)

0

精彩评论

暂无评论...
验证码 换一张
取 消