Is there a straightforward way of determining the number of decimal places in a(n) integer/double value in PHP? (that is, without using explode
)
$twoDecNum = sprintf('%0.2f', round($number, 2)); The rounding correctly rounds the number and the sprintf forces it to 2 decimal places if it happens to to be only 1 decimal place after rounding.
The round() function rounds a floating-point number. Tip: To round a number UP to the nearest integer, look at the ceil() function.
To convert a fraction to a decimal, you remember that 1/2 is the same as 1 ÷ 2 and work out the division. Those two principles come in handy when you convert mixed numbers to decimals because you can keep the whole number, work the division to change the fraction to a decimal, and then add the two together.
$str = "1.23444"; print strlen(substr(strrchr($str, "."), 1));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With