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)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay