Method: 1. Use "decimal 0"; 2. Use "floatval(decimal)"; 3. Use "rtrim(rtrim(decimal,'0'),'.')"; 4. Use "preg_replace('/[.]$/','',preg_replace('/0 $/','', decimal)".
This Tutorial operating environment: Windows 7 system, PHP 7.1 version, DELL G3 computer
Today we introduce several methods to remove the 0 at the end of the decimal point:
Method 1. Add directly 0
##Because PHP is a weak type, it can directly perform mathematical operations and convert them into numbers.<?php echo '100.00' + 0 ."<br>"; echo '100.01000' + 0 ."<br>"; echo '100.10000' + 0 ."<br>"; ?>
100 100.01 100.1
Method 2, use floatval() to convert to floating point
<?php echo floatval('100.00')."<br>"; echo floatval('100.01000')."<br>"; echo floatval('100.10000')."<br>"; ?>
100 100.01 100.1
Method 3, use rtrim ()Function
<?php echo rtrim(rtrim('100.00', '0'), '.')."<br>"; echo rtrim(rtrim('100.01000', '0'), '.')."<br>"; echo rtrim(rtrim('100.10000', '0'), '.')."<br>"; ?>
100 100.01 100.1
Method 4. Use regular expression
正则表达式说明: /0+$/ 去掉末尾多余的0 /[.]$/ 去掉末尾的. <?php echo preg_replace('/[.]$/', '', preg_replace('/0+$/', '', '100.00'))."<br>"; echo preg_replace('/[.]$/', '', preg_replace('/0+$/', '', '100.1000'))."<br>"; echo preg_replace('/[.]$/', '', preg_replace('/0+$/', '', '100.010203000'))."<br>"; ?>
100 100.1 100.010203
PHP Video Tutorial"
The above is the detailed content of How to remove extra 0 after decimal point in php. For more information, please follow other related articles on the PHP Chinese website!