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

How to convert percentage to decimal in php

藏色散人
Release: 2023-03-05 18:40:01
Original
2680 people have browsed it

How to convert percentage to decimal in php: First create a PHP sample file; then define a percentage; then convert the percentage to decimal through the "(float)$a/100;" method; finally output the converted value through echo of decimals is enough.

How to convert percentage to decimal in php

Recommended: "PHP Video Tutorial"

PHP percentage to decimal, php decimal conversion percentage function A Xing Xiao Stack

PHP percent to decimal:

$a = "20.544545%";
echo (float)$a/100;
Copy after login

Result: 0.20544545

php Decimal to percent conversion function:

No digits required:

$n*100.'%';

If there are requirements for the number of novels

(1) You can use sprintf(" .2f", $n*100). '%';

(2) Round first, then convert

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

To use the rounding function round()

Example:

  $hhl=round(($hh/$totalpj)*100).’%';//好评率
  $lhl=round(($lh/$totalpj)*100).’%';//良好率
  $ybl=round(($yb/$totalpj)*100).’%';//一般率
  $cl=round(($c/$totalpj)*100).’%';//差率
  $hcl=round(($hc/$totalpj)*100).’%';//很差率
Copy after login

The above is the detailed content of How to convert percentage to decimal 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