这是我从PHP脚本中看到的输出:
Cash Completions (Purchase)
Product A: £435.60
Product B: £38.40
Product C: £0.00
Product D: £3,349.87
Product E: £559.38
Product F: £0.00
Product G: £0.00
Product H: £0.00
TOTAL COSTS: £1,036.38
看看最后一行“TOTAL COSTS”。您将看到它不会累加到上面所有行的总和。
以下是用于计算此内容的PHP脚本:
Cash Completions (Purchase)
Product A: <?php echo '£'.$extra->a;?>
Product B: <?php echo '£'.$extra->b;?>
Product C: <?php echo '£'.$extra->c;?>
Product D: <?php echo '£'.$extra->d;?>
Product E: <?php echo '£'.$extra->e;?>
Product F: <?php echo '£'.$extra->f;?>
Product G: <?php echo '£'.$extra->g;?>
Product H: <?php echo '£'.$extra->h;?>
TOTAL COSTS: <?php echo '£'.number_format($extra->a + $extra->b + $extra->c + $extra->d + $extra->e + $extra->f + $extra->g + $extra->h, 2);?>
$extra
变量是表示MySQL Resultset的对象。正如您所看到的,单个产品的输出是正确的,但由于某种原因,总数是里程数。
有什么想法吗?
谢谢!
答案 0 :(得分:5)
答案 1 :(得分:2)
问题在于数字:
3,349.87
应该是:
3349.87
数字3,349.87不是PHP的正确数字。 PHP只识别和翻译点('。')而不是逗号(',')
你可以这样做:
$total = (float)str_replace(",","",$extra->a) + (float)str_replace(",","",$extra->b) + ...
TOTAL COSTS: <?php echo '£'.number_format($total, 2); ?>