Function to count decimal places with PHP
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
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.
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.
精彩评论