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?
Complicated way but works:
the solutions above are the optimal way but in case you want to have your own you could use this. What this algorithm does it starts at the end of string and checks if its 0, if it is it sets to empty string and then goes to the next character from back untill the last character is > 0
There is nothing that can't be fixed with a simple regex ;)
http://xkcd.com/208/
This is my solution. I want to keep ability to add thousands separator
If you want to remove the zero digits just before to display on the page or template.
You can use the sprintf() function
Strange, when I get a number out of database with a "float" type and if my number is ex. 10000 when I floatval it, it becomes 1.
I've tested all the solutions above but didn't work.