我可以做到這一點用printf()可以echo指定一個浮點數嗎?
printf ("Total amount of order is %.2f", $total);
我可以做使用回聲一樣,或者我必須用printf()?
我可以做到這一點用printf()可以echo指定一個浮點數嗎?
printf ("Total amount of order is %.2f", $total);
我可以做使用回聲一樣,或者我必須用printf()?
你可以使用這個..
$total= 100.123;
echo "Total amount of order is ". round($total, 2);
輸出:
Total amount of order is 100.12
單獨使用回聲?不可以。您將在回顯字符串中包含number_format
以獲得相同的功能。
echo "Total amount of order is ".number_format($total,2);
這就是我想要的。謝謝詹姆斯! –
您不能使用echo指定格式。
但是如果你需要結果分配給字符串,可以簡單的使用功能sprintf
:
$var = sprintf ("Total amount of order is %.2f", $total);
echo $var;
哦,我也可以使用sprintf()。謝謝Marcin! –
不錯又短!非常感謝next2u! –
如果總數達到一個整數,它將不輸出到2個小數位。輪(120.00,2)= 120,而不是120.00。單個數字相同,120.20將變爲120.2。 –
@JamesHunt,你是對的..謝謝 – next2u