2016-11-05 61 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