1

How can I get the fractional part of a number?

For example, I have a list of floats num = [12.73, 9.45] and want to get only the numbers after the decimal point, 73 and 45 in this case. How do I go about doing this?

Merp
  • 118
  • 1
  • 10
  • 3
    What have you tried so far? Perhaps make the values a string and call `.split('.')`? – ti7 Nov 25 '20 at 22:24
  • Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). "Show me how to solve this coding problem?" is off-topic for Stack Overflow. You have to make an honest attempt at the solution, and then ask a *specific* question about your implementation. Stack Overflow is not intended to replace existing tutorials and documentation. – Prune Nov 25 '20 at 22:27
  • This website is no substitute for reading one of the (huge!) number of Python language tutorials available, – martineau Nov 25 '20 at 23:13
  • Does this answer your question? [How to get numbers after decimal point?](https://stackoverflow.com/questions/3886402/how-to-get-numbers-after-decimal-point) – Gino Mempin Nov 25 '20 at 23:51

3 Answers3

4

One approach is using pure(ish) maths.

The short answer:

num = [12.73, 9.45]

[int((f % 1)*100) for f in num]

>>> [73, 44]

Explanation:

The modulo operator returns the remainder once whole division is complete (to over-simplify).

Therefore this, returns the decimal value; the fractional part of the number.

12.73 % 1

>>> 0.7300000000000004

To get the decimal value as a integer, you can use:

int((12.73 % 1)*100)

>>> 73

Just wrap this in a loop for all required values ... and you have the 'short answer' above.

S3DEV
  • 8,768
  • 3
  • 31
  • 42
1
num = [12.73, 9.45];
result = list(map(lambda x: int(str(x).split('.')[1]),num))
print(result)
Asad
  • 90
  • 2
  • 9
1

and want to get only the numbers after the period,

There is no such thing. Numbers don't have digits; the string representation of the numbers has digits. And even then, floating-point numbers are not precise; you may be shown 0.3 in one context and 0.30000000000000004 in another, for the same value.

It sounds like what you are actually after is the fractional part of the numbers. There are many ways to do this, but they all boil down the same idea: it is the result when you divide (as a floating-point number) the input by 1.

For a single value, it looks like:

fractional_part = value % 1.0

or

# This built-in function performs the division and gives you
# both quotient and remainder.
integer_part, fractional_part = divmod(value, 1.0)

or

import math
fractional_part = math.fmod(value, 1.0)

or

import math
# This function is provided as a special case.
# It also gives you the integer part.
# Notice that the results are the other way around vs. divmod!
fractional_part, integer_part = math.modf(value)

To process each value in a list in the same way, use a list comprehension.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153