在php中从字符串中删除余数
简单的问题。我有以下代码:在php中从字符串中删除余数,php,math,printing,echo,Php,Math,Printing,Echo,简单的问题。我有以下代码: total = 41 win = 48 echo ($total/$win) * 100 ; 打印 85.416667 我需要去掉剩下的部分,这样它就能打印出来:85% 根据要将数字四舍五入的方式,您可能需要将floor()替换为 地板()(9.4→ 9, 9.7 → (九) round()(9.4→ 9, 9.7 → (十) ceil()(9.4→ 10, 9.7 → (十) 您可以使用以下功能: echo floor(($total/$
total = 41
win = 48
echo ($total/$win) * 100 ;
打印
85.416667
我需要去掉剩下的部分,这样它就能打印出来:85%
根据要将数字四舍五入的方式,您可能需要将floor()
替换为
(9.4→ 9, 9.7 → (九)地板()
(9.4→ 9, 9.7 → (十)round()
(9.4→ 10, 9.7 → (十)ceil()
echo floor(($total/$win) * 100) ;
使用该函数
<?php
$total = 41;
$win = 48;
echo round(($total/$win)*100).' %';
?>
优雅的方法是使用字符串
number_format(float $number, int $decimals, string $dec_point, string $thousands_sep);
像这样:
<?php
$total = 41;
$win = 48;
echo number_format(($total/$win)*100,0,'.').' %';
?>
删除余数(总是向下舍入),或将结果舍入到最接近的整数?如下图所示,但在我的例子中,我必须使用floor()然后使用du so number_格式(floor($total/$win)*100)、0、'、'、').%;我还为number_format函数添加了缺少的第四个参数。完全忘记了它需要2个或4个参数。哇,3年了。我刚认识ceil。谢谢你,伙计!
<?php
$total = 41;
$win = 48;
echo number_format(($total/$win)*100,0,'.').' %';
?>