[英]Remove useless zero after number using php code
我們正在使用以下代碼在磁網站上顯示運費:
<?php echo "Selling Price + " . $_excl . " Delivery "; ?>
$_excl
將返回值。
其顯示結果為10.00、20.00 ...等。
我想從“ 10.00”中刪除.00 ,並僅顯示10。
我嘗試了以下代碼:
echo "Selling Price + " . round($_excl,0) . " Delivery ";
echo "Selling Price + " . round($_excl) . " Delivery ";
echo "Selling Price + " . $_excl + 0 . " Delivery ";
沒有任何幫助,請給我更新的代碼
您可以使用(int)
, number_format()
或round()
或intval()
$num = '10.00';
echo (int)$num ."\n"; //print 10
echo number_format($num,0) ."\n"; //print 10
echo round($num,0) ."\n"; // print 10
echo intval($num) ."\n"; // print 10
所以你的情況
echo "Selling Price + " . (int)$_excl . " Delivery " . "\n";
echo "Selling Price + " . number_format($_excl,0) . " Delivery " . "\n";
echo "Selling Price + " . round($_excl,0) . " Delivery " . "\n";
echo "Selling Price + " . intval($_excl) . " Delivery " . "\n";
您可以使用number_format() :
echo "Selling Price + " . number_format($_excl, 0) . " Delivery ";
$num + 0
可以解決問題。
echo 125.00 + 0; // 125
echo '125.00' + 0; // 125
echo 966.70 + 0; // 966.7
在內部,這等效於使用(float)$num
或floatval($num)
強制轉換為float,但是我發現它更簡單。
更新資料
echo "Selling Price + " . ($_excl + 0) . " Delivery ";
嘗試
echo "Selling Price + " . (int)$_excl . " Delivery ";
例如
$f = "10.00";
echo (int)$f; //gives 10
希望它幫助:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.