如何实现百分比的计算:
$x=3;
$y = 100\$x; // 3.333..
$x*$y - and here I need to get 100 without Observational error
有什么想法吗?
答案 0 :(得分:38)
您的反斜杠(\
)不是分割符号。您必须使用分区/
符号。
以下是使用number_format()
的示例,该示例将舍入到两个小数位(百分位)。
$x = 3;
$y = 15;
$percent = $x/$y;
$percent_friendly = number_format( $percent * 100, 2 ) . '%'; // change 2 to # of decimals