Question

$f = sprintf ("%.1f",$per);
echo "You hit".$f."

By this code i can show only one digit after floating point.But when my result is 100%,then it shows 100.0%. I want to show 100%. how could i do this????

Was it helpful?

Solution 2

$per = 100;
$f = sprintf ("%.1f",$per);
$rounded = round($f, 1);
echo $rounded;

OTHER TIPS

The easiest way to do this is to cast resulting string into float again after formatting, i.e.:

$f=100.44;
$g=100;
var_dump((float)sprintf('%.1f', $f));//100.4
var_dump((float)sprintf('%.1f', $g));//100
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top