4

I have a function that takes a float as an input and the output is only the decimal part. For example, get_decimal(4.45) should return 0.45, and we should only return positive figures.

I made the following code:

def get_decimal(n): 
    try:
        return float('0.'+(str(n).split('.',1))[1])
    except:
        return 0

And this code almost works, but it doesn't give the whole answer. For example get_decimal(4.566666678258757587577) only returns:

0.566666678258757 

instead of:

0.566666678258757587577

Is there a way to get the whole number?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
zzz247
  • 97
  • 1
  • 7
  • Does this answer your question? [How to get numbers after decimal point?](https://stackoverflow.com/questions/3886402/how-to-get-numbers-after-decimal-point) – Tomerikoo Jan 28 '21 at 10:26
  • Your method works fine. The reason it doesn't seem like the same number is just because of `print`ing limitations. Try `get_decimal(4.566666678258757587577) * 1000 % 1` and see that you have all digits. Doing a simple `print(4.566666678258757587577)` will also give just `4.566666678258757` and not the full number... – Tomerikoo Jan 28 '21 at 10:32

3 Answers3

8

Use the modulus:

inp = 4.566666678258757587577
output = inp % 1
print(output)    # prints 0.566666678259

Note that Python's print() function usually attempts to display a more human readable form of a floating point number. So, while the printed value appears to stop after 12 digits, there is more precision beyond that not shown.

Consider:

print((output * 100000) % 1)   # prints 0.667825875724
                               #   4.566666678258757587577  <- original input
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Thank you very much for this solution! But it looks like it doesn't give the entire decimal part. For example, the output should be 0.566666678258757587577 – zzz247 Jan 28 '21 at 10:17
  • @Tim I think there was a way to tweak the precision of the print() statement though I am not sure. – user79161 Jan 28 '21 at 10:20
  • @zzz247 The issue here is Python's `print()` function, which has rules it uses for determining how to _display_ a floating point number. As my updated answer shows, the full precision is still there, you just don't see it in the console. – Tim Biegeleisen Jan 28 '21 at 10:20
  • Your solution works fine. But do you have an idea of why my solution does not work? – zzz247 Jan 28 '21 at 10:22
  • 1
    Define "work" here. Your solution probably _does_ work, insofar that you end up with the decimal component of your input float. The real litmus test for you would be to use that decimal component and do some math. Does your calculation give the expected result? Then your approach is valid. – Tim Biegeleisen Jan 28 '21 at 10:23
0

you can try this :

output = round(inp-int(inp),abs(decimal.Decimal(str(inp)).as_tuple().exponent)
Belhadjer Samir
  • 1,461
  • 7
  • 15
0

you can use Decimal but in that case you need to set your input as string:

from decimal import *

def get_decimal(n): 
    try:
        return Decimal('0.'+(str(n).split('.',1))[1])
    except:
        return 0
print(get_decimal("4.5666666782587575875779"))

output:

0.5666666782587575875779
SM Abu Taher Asif
  • 2,221
  • 1
  • 12
  • 14