DEV Community

Cover image for Displaying the Integer Part of a Value in Python
Daniel Nogueira
Daniel Nogueira

Posted on

2

Displaying the Integer Part of a Value in Python

We can check the integer part of a real number, and one of the ways to do that is using the trunc() method of the math library.

Let's import the method:

from math import trunc
Enter fullscreen mode Exit fullscreen mode

Then we read a real number and display its truncated value on the screen:

n = float(input('Real number: '))

print(f'Integer part: {trunc(n)}')
Enter fullscreen mode Exit fullscreen mode

Another way is simply using the int() function to display the value in its entirety.

print(f'Integer part: {int(n)}')
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more