Home  >  Article  >  Backend Development  >  How to convert decimal to percent in php

How to convert decimal to percent in php

王林
王林Original
2021-10-18 16:54:252907browse

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.

How to convert decimal to percent in php

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.&#39;%&#39;; 
}

If there are requirements for the number of decimal places

sprintf("%01.2f", $n*100).&#39;%&#39;;

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more