2014-04-10 53 views
0

我试图修改伪购物车(基于用户的元数据,而不是数据库中存储的值它的阵列)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(),使其工作,但无法找到一个解决方案。

+0

或许有点贫民窟建议,但你有没有试过类似'(10 * 100)'? – Helpful

+1

您的整个'foreach'循环看起来很可疑。您将覆盖'$ price'的值,然后使用该结果进行下一次计算,但让我感到困惑的是取代小数点。这应该是基本的算术运算,但看起来你正在做一些奇怪的字符串转换,这会破坏你的结果。 –

+0

尝试http://stackoverflow.com/questions/5139793/php-unformat-money这个解决方案 –

回答

0

我认为通过此线

$price = str_replace('.', '', $price['final_price']); 

300 + 500 + “1000”;

你的号码,如1000被转换成字符串,然后你的总变成801

你必须转换成浮在适当的方式在本

PHP: unformat money

+0

我跟随了线程,发现了一个解决方法:用$ price = floatval替换str_replace和floatval(preg_replace('/ [^ \ d \'] /','',$ price ['final_price']));并将总数更改为$ total = number_format($ total,2);现在货币显示正确。非常感谢你! – hanasuz