Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何格式化数字?php函数_Php_Numbers_Format - Fatal编程技术网

如何格式化数字?php函数

如何格式化数字?php函数,php,numbers,format,Php,Numbers,Format,我需要帮助格式化来自get\u order\u total()的结果,它需要有逗号和句点,比如1000.00,因为我处理的是数字 我是PHP新手,以下是我的代码: function get_order_total(){ $max=count($_SESSION['cart']); $sum=0; for($i=0;$i<$max;$i++){ $pid=$_SESSION['cart'][$i]['productid'];

我需要帮助格式化来自
get\u order\u total()
的结果,它需要有逗号和句点,比如1000.00,因为我处理的是数字

我是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;  
   }
函数get\u order\u total(){ $max=计数($_会话['cart']); $sum=0; 对于($i=0;$i),这可以通过使用来完成。例如:

$num = 1000;
echo number_format($num,2); // 1,000.00
使用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 number_format($sum);  
   }
函数get\u order\u total(){ $max=计数($_会话['cart']); $sum=0;
对于($i=0;$i您已经看过php的
number\u format
函数了吗?您可以使用php的number\u format()函数来实现这一点。为什么使用javascript标记?甚至返回正确的答案……谢谢!!它起作用了。哈哈。