Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
$f = sprintf ("%.1f",$per); echo "You hit".$f."
通过这段代码,我只能在浮点数之后显示一位数字。但是当我的结果是 100% 时,它会显示 100.0%。我想显示 100%。我怎么能这样做????
最简单的方法是在格式化后将结果字符串再次转换为浮点数,即:
$f=100.44; $g=100; var_dump((float)sprintf('%.1f', $f));//100.4 var_dump((float)sprintf('%.1f', $g));//100
$per = 100; $f = sprintf ("%.1f",$per); $rounded = round($f, 1); echo $rounded;