简体   繁体   中英

Remove useless zero after number using php code

we are using this code for displaying shipping charges in magneto site :

<?php echo "Selling Price + " . $_excl . " Delivery "; ?>

where $_excl will return the value.

its displaying results as 10.00, 20.00...etc.

I want to remove .00 from "10.00" & display only 10.

I checked here1 & here2

I tried below codes :

echo "Selling Price + " . round($_excl,0) . " Delivery ";
echo "Selling Price + " . round($_excl) . " Delivery ";
echo "Selling Price + " . $_excl + 0 . " Delivery ";

nothing worked for me, Please give me updated code for this

You can either use (int) , number_format() or round() or 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

live sample

So in your case

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";

live sample

您可以使用number_format()

echo "Selling Price + " . number_format($_excl, 0) . " Delivery ";

$num + 0 does the trick.

echo 125.00 + 0; // 125
echo '125.00' + 0; // 125
echo 966.70 + 0; // 966.7

Internally, this is equivalent to casting to float with (float)$num or floatval($num) but I find it simpler.

Update

echo "Selling Price + " . ($_excl + 0) . " Delivery ";

Try

echo "Selling Price + " . (int)$_excl . " Delivery ";

eg

$f = "10.00";
echo (int)$f; //gives 10

hope it hepls :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM