2014-02-27 62 views
0

我需要帮助格式化来自get_order_total()的结果,它需要有逗号和句点,如1,000.00,因为我正在处理数字。如何格式化数字? php函数

我是新来的PHP,这里是我的代码:

function get_order_total(){ 
     $max=count($_SESSION['cart']); 
     $sum=0; 
     for($i=0;$i<$max;$i++){ 
     $pid=$_SESSION['cart'][$i]['productid']; 
     $q=$_SESSION['cart'][$i]['qty']; 
     $price=get_price($pid); 
     $sum+=$price*$q; 
     } 
    return $sum; 
    } 

谢谢

+0

你看的PHP'number_format'功能 –

+0

你可以做到这一点使用PHP的number_format()函数。 http://nl1.php.net/number_format – Daan

+0

为什么javascript标签? –

回答

0

使用php number_format
试试这个代码:

function get_order_total(){ 
     $max=count($_SESSION['cart']); 
     $sum=0; 
     for($i=0;$i<$max;$i++){ 
     $pid=$_SESSION['cart'][$i]['productid']; 
     $q=$_SESSION['cart'][$i]['qty']; 
     $price=get_price($pid); 
     $sum+=$price*$q; 
     } 
    return number_format($sum); 
    } 
+0

谢谢!有效。哈哈。 – NewbieCoder999