Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count decimal places in a float [duplicate]

In python, I import a file that has different length decimal places in the numbers, such as 7.2 or 7.2332 Is it possible to find out the number of decimal places in the number, if so, how? All the question I can find on SO are about formatting to a specific decimal place, but thats not what I am doing.

The application is to use qdoublespinbox and set the decimal places to only the required amount.

Edit:

In context, I am able to print the number, which will print 7.2, or 5.42422 etc. If the command prompt prints the correct digits (ie. doesnt print everything in the memory allocation of a float) is there any way to get that information.

like image 311
user-2147482637 Avatar asked Oct 07 '14 08:10

user-2147482637


1 Answers

Assuming that 'import a file' means your decimals are string in a file, you can use reverse and find:

>>> f = "7.2332"
>>> f[::-1].find('.')
4
>>> f = "7.20"
>>> f[::-1].find('.')
2
like image 66
fredtantini Avatar answered Oct 18 '22 18:10

fredtantini