开发者

number_format in a number_format doesn't work

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜