17

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.

user-2147482637
  • 2,115
  • 8
  • 35
  • 56
  • 1
    You can't. It's impossible (unless you're willing to get answers you didn't expect). – Veedrac Oct 07 '14 at 08:31
  • 2
    When using computers, in particular floats, just because you set a number to a certain number of decimal places doesn't mean that it actually has that many. This is because there are some numbers that you cannot replicate, such as `0.1` which is actually `0.100000000000000005551115123126` – Ffisegydd Oct 07 '14 at 08:33
  • I guess the quick downvotes means there is some problem with the wording, so i asked if it is possible. Not sure what answers i dont expect would be, but I dont see why the question warrants downvotes, the answer is then its impossible – user-2147482637 Oct 07 '14 at 08:33
  • @Ffisegydd I added an edit to clarify – user-2147482637 Oct 07 '14 at 08:36
  • @user1938107 I downvoted because of both the lack of attempt to find duplicates and not giving important (relevant) information. – Veedrac Oct 07 '14 at 08:36
  • `len(str(7.2332 ).split(".")[-1])` – Padraic Cunningham Oct 07 '14 at 08:37
  • @Veedrac Well its hard to argue attempts to find duplicates, as searching 'python decimals in a float' did not return any results besides formatting, and SO didnt recommend similar items. That said, the other answer does answer this – user-2147482637 Oct 07 '14 at 08:44
  • @user1938107 Try searching your title in Google ("count decimal places in a float python") – Veedrac Oct 07 '14 at 08:45
  • @Veedrac Uh, embarrassing, deserves the downvotes for sure – user-2147482637 Oct 07 '14 at 08:47

2 Answers2

43

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
fredtantini
  • 15,966
  • 8
  • 49
  • 55
  • 3
    A simple casting works for float parameter that was actually requested: `f = 7.2332; str(f)[::-1].find('.')`. – Gürol Canbek Jan 03 '17 at 08:13
  • 2
    Thanks for this! It worked for me in the future and with Python 3.x - a note that if there are no decimals, it will return `-1`. For some reason, the post that this was closed for and marked a duplicate of, did *not* work. So, again, thanks for this post! :D – BruceWayne Feb 06 '18 at 15:54
  • This is great! Can someone explain how the [::-1] indexing works? Looks to me like it reverses the string elements then searches from the new "beginning" – grego Aug 24 '21 at 00:49
  • Ultra simple and effective, thank you sir! – NoahVerner Jul 10 '22 at 17:02
3

Convert to a string and find the position of the decimal point, relative to the length of the string.

TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85