Reputation: 2158
Is there a native PHP function which tells me how many decimal places a number has, or will I have to make my own?
IE:
0.8 -> 1
0.2345 -> 4
0.894 -> 3
Upvotes: 3
Views: 1285
Reputation: 526803
You'll have to make your own if you really want that function. It shouldn't be all that hard, though - just convert to a string, explode()
on the .
, and then take the length of the second element in the array.
Beware of floating point inaccuracy, though.
Upvotes: 5
Reputation: 375674
Floating point numbers are stored in binary, they don't "have" any number of decimal places. They are converted to decimal representation as part of printing them. How many places are printed depends on the formatting function used to print them.
Upvotes: 0