Php number_Format返回1.00

人气:143 发布:2023-01-03 标签: php mysql numerical

问题描述

我已经在MySql中存储了总数为(十进制16,2)1423.28

我做了一些计算后从PHP中得到了显示:

function calculate_balance($total){

 //get total paid from another function
 $total_paid = ...

 if ($total_paid == 0){
     return $total;
 }else{
     return $total-$total_paid
 }

} 

 $balance = calculate_balance($total);
 echo number_format($balance, 2); //returns 1.00

我已尝试

  number_format((float)$balance, 2);
  number_format(floatval($balance), 2); 

更新

var_dump($balance)

我得到了以下输出。

string(8) "1,423.28" float(152) string(6) "252.00" string(6) "247.50" string(6) "247.50" string(6) "247.50" string(6) "549.90" string(6) "495.00" float(0) string(6) "284.76" float(265)

对于小于1,000的值,它在没有number_format()的情况下工作正常。 例如:如果余额等于252.00

 echo $balance;

输出

252.00

推荐答案

您的函数返回1,423.28?这不是浮点型,因为浮点型从不包含逗号作为千位分隔符。

PHP将其解释为1,因为它在逗号处"中断"。

去掉逗号就可以了!

$balance = str_replace(',', '', $balance);

20