php將小數(shù)轉(zhuǎn)為百分數(shù)的方法:【function xxx($n) {return $n * 100.'%'}】。如果要將百分數(shù)轉(zhuǎn)為小數(shù),可以使用【(float)$a/100;】來實現(xiàn)。
本文操作環(huán)境:windows10系統(tǒng)、php 7、thinkpad t480電腦。
PHP百分號轉(zhuǎn)小數(shù):
<?php $a = "20.544545%"; echo (float)$a/100; ?>
php 小數(shù)轉(zhuǎn)換百分數(shù)函數(shù):
function xx($n) { return $n*100.'%'; }
如果小數(shù)位數(shù)有要求
sprintf("%01.2f", $n*100).'%';
先 四舍五入,后轉(zhuǎn)換
$str=round(5.055, 2); // 5.06 $str1=$str."%"; //5.06%
推薦學(xué)習(xí):php培訓(xùn)