Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: get number of decimal digits

Tags:

php

decimal

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)

like image 218
Erwin Avatar asked Mar 12 '10 02:03

Erwin


People also ask

How can I get 2 digit decimal numbers in PHP?

$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.

How can I limit float to 2 decimal places in PHP?

The round() function rounds a floating-point number. Tip: To round a number UP to the nearest integer, look at the ceil() function.

How do you find a decimal number?

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.


1 Answers

$str = "1.23444"; print strlen(substr(strrchr($str, "."), 1)); 
like image 101
ghostdog74 Avatar answered Sep 20 '22 15:09

ghostdog74