How to get the decimal part of a number in PHP

4 Answers

0 votes
$f = 53.8970; 

$n = floor($f);     
$decimal_part = $f - $n; 

echo $decimal_part;




/*
run:

0.897

*/

 



answered Jun 15, 2022 by avibootz
0 votes
$f = -93.41790;
$f = abs($f);

$n = floor($f);     
$decimal_part = $f - $n; 

echo $decimal_part;




/*
run:

0.4179

*/

 



answered Jun 15, 2022 by avibootz
0 votes
$f = 93.41790;

$s = (string)$f; 

echo substr($s, strpos($s, "."));




/*
run:

.4179

*/

 



answered Jun 15, 2022 by avibootz
0 votes
$f = 93.41790;

list($whole, $decimal_part) = explode('.',  $f);

echo $decimal_part;




/*
run:

4179

*/

 



answered Jun 15, 2022 by avibootz

Related questions

3 answers 368 views
2 answers 160 views
2 answers 197 views
2 answers 200 views
1 answer 160 views
1 answer 113 views
1 answer 138 views
...