Php number_Format返回1.00 [英] PHP number_format returns 1.00

查看:0
本文介紹了Php number_Format返回1.00的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我已經在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);

這篇關于Php number_Format返回1.00的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持IT屋!

查看全文
登錄 關閉
掃碼關注1秒登錄
發送“驗證碼”獲取 | 15天全站免登陸
全免费A级毛片免费看无码播放