Home > Backend Development > PHP Problem > How to convert decimal to percent in php

How to convert decimal to percent in php

王林
Release: 2023-03-13 11:16:01
Original
2966 people have browsed it

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; 
?>
Copy after login

php decimal conversion percentage function:

function xx($n) 
{ 
return $n*100.&#39;%&#39;; 
}
Copy after login

If there are requirements for the number of decimal places

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

Round first, then convert

$str=round(5.055, 2); // 5.06 $str1=$str."%"; //5.06%
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template