2016-11-05 58 views
-1

在十進制數字中如何除去小數點後的多餘零以及如何避免舍入數字。 我曾嘗試下面的代碼如何在十進制值中插入逗號並刪除多餘的零

$example = "123456"; 
$exmaple2= "55.369"; 
$subtotal = number_format($example, 2, '.', ','); 
$subtotal1 = number_format($exmaple2, 2, '.', ','); 
echo $subtotal."<br/>"; 
echo $subtotal1; 

得到輸出

123,456.00 
55.37 

預期輸出

123,456 
55.369 

回答

0

當與數字打交道,除去雙引號至少區分串數字。如果您想動態使用該變量,請驗證其浮點數或數字。

$example = 123456; 
$exmaple2= 55.369; 
$subtotal = (is_float($example) == true ? number_format($example, 3, '.', ',') : number_format($example, 0, '.', ',')); 
$subtotal1 = (is_float($exmaple2) == true ? number_format($exmaple2, 3, '.', ',') : number_format($exmaple2, 0, '.', ',')); 
echo $subtotal."<br>"; 
echo $subtotal1; 
0

嘗試此

$example = "123456"; 
$exmaple2= "55.369"; 
$subtotal = number_format($example, 0, '.', ','); 
$subtotal1 = number_format($exmaple2, 3, '.', ','); 
echo $subtotal."\n"; 
echo $subtotal1; 

注number_format()的第二參數

0

嘗試此

$example = "123456"+0; 
//$example = "55.369"+0; 
if (is_float($example)) {  
    $subtotal = number_format($example, 3, '.', ',')+0; 
    } else { 
    $subtotal = number_format($example, 0, '.', ','); 
    } 


echo $subtotal."<br/>"; 
+0

正在打印的小計值123,其中,因爲我需要123,456其他明智的代碼是每個fect – atk

+0

檢查我編輯的答案。 – shubham715

+0

它是非常工作非常感謝你 – atk