如何格式化数字?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标记?甚至返回正确的答案……谢谢!!它起作用了。哈哈。