我试图修改伪购物车(基于用户的元数据,而不是数据库中存储的值它的阵列)PHP文件中错误地显示货币> 1000为0.01显示货币值大于1000
这里是块我想改变:
public function get_cart_value()
{
$cart = $this->cart;
$prices = array();
$cart = apply_filters('ss_mod_cart_value', $cart);
foreach ($cart['products'] as $product)
{
array_push($prices, array('price'=>trim($product['price']), 'discount' => trim($product['discount'])));
}
$total = 0;
foreach($prices as $price)
{
$price = $this->calc_discount($price['price'], $price['discount']);
$price = str_replace('.', '', $price['final_price']);
$total += $price;
}
$total = number_format($total/100, 2);
return $total;
}
的购物车按钮,它的工作原理上正确显示的项目不超过4个位数的总价值,例如:
300 + 500 = 800
但
300 + 500 + 1000 = 800.01
而不是1,800
我一直在试图改变number_format()
,使其工作,但无法找到一个解决方案。
或许有点贫民窟建议,但你有没有试过类似'(10 * 100)'? – Helpful
您的整个'foreach'循环看起来很可疑。您将覆盖'$ price'的值,然后使用该结果进行下一次计算,但让我感到困惑的是取代小数点。这应该是基本的算术运算,但看起来你正在做一些奇怪的字符串转换,这会破坏你的结果。 –
尝试http://stackoverflow.com/questions/5139793/php-unformat-money这个解决方案 –