Home > Article > Backend Development > How to convert decimal to percent in php
php method to convert decimals to percentages: [function xxx($n) {return $n * 100.'%'}]. If you want to convert a percentage to a decimal, you can use [(float)$a/100;] to achieve this.
The operating environment of this article: windows10 system, php 7, thinkpad t480 computer.
PHP percent to decimal conversion:
<?php $a = "20.544545%"; echo (float)$a/100; ?>
php decimal conversion percentage function:
function xx($n) { return $n*100.'%'; }
If there are requirements for the number of decimal places
sprintf("%01.2f", $n*100).'%';
Round first, then convert
$str=round(5.055, 2); // 5.06 $str1=$str."%"; //5.06%
Recommended learning: php training
The above is the detailed content of How to convert decimal to percent in php. For more information, please follow other related articles on the PHP Chinese website!