$per = 100;
$f = sprintf ("%.1f",$per);
$rounded = round($f, 1);
echo $rounded;
How could i show only one digit after floating point
Domanda
$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????
Soluzione 2
Altri suggerimenti
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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow