I'm trying to find a fast way to remove zero decimals
from number values like this:
echo cleanNumber('125.00');
// 125
echo cleanNumber('966.70');
// 966.7
echo cleanNumber(844.011);
// 844.011
Does exists some optimized way to do that?
I'm trying to find a fast way to remove zero decimals
from number values like this:
echo cleanNumber('125.00');
// 125
echo cleanNumber('966.70');
// 966.7
echo cleanNumber(844.011);
// 844.011
Does exists some optimized way to do that?
This Code will remove zero after point and will return only two decimal digits.
$number=1200.0000;
str_replace('.00', '',number_format($number, 2, '.', ''));
Output will be: 1200
$num + 0
does the trick.Internally, this is equivalent to casting to float with
(float)$num
orfloatval($num)
but I find it simpler.you could just use the
floatval
functionTypecast to a
float
.For everyone coming to this site having the same problem with commata instead, change:
to:
If there are two zeros to be removed, then change to:
More here: PHP number: decimal point visible only if needed