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?
You can use:
You should cast your numbers as floats, which will do this for you.
Output of this will be what you are looking for.
Due to this question is old. First, I'm sorry about this.
The question is about number xxx.xx but in case that it is x,xxx.xxxxx or difference decimal separator such as xxxx,xxxx this can be harder to find and remove zero digits from decimal value.
And here is the code for test.
This is a simple one line function using rtrim, save separator and decimal point :
This is what I use:
N.B. This assumes
.
is the decimal separator. It has the advantage that it will work on arbitrarily large (or small) numbers since there is no float cast. It also won't turn numbers into scientific notation (e.g. 1.0E-17).Simply adding
+
to your string variable will cause typecast to (float) and removes zeros: