簡體   English   中英

php函數:number_format()

[英]php function: number_format()

$result = number_format($x, 2, '.', ','); //Will do the following correctly
115255 = 115,225.00
115255.4 = 115,225.40
115255.40 = 115,225.40
115255.455 = 115,255.46

但是我需要用戶在小數點后輸入兩位以上的數字時,不要只將它們切成兩位小數並按原樣使用它...

115255.455 = 115,255.455
115255.4557 = 115,255.4557

我可以那樣做嗎?

if($x == number_format($x, 3)) //I will do it in while loop later, lets test 3 now
    $result = number_format($x, 3, '.', ',');
else $result = number_format($x, 2, '.', ',');

前一個如果條件永遠不起作用,否則只能起作用

一種非傳統的方法:

$parts = explode(".", $x);
$integerPart = number_format($parts[0], 0, '', ',');
$result = $integerPart.".".$parts[1];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM