trunc(): integer part of a number

trunc() function returns integer part of a number .
Here are some examples with different type of numbers.
import math
print(math.trunc(34.56))   # 34
print(math.trunc(34.21))   # 34
print(math.trunc(34.99))   # 34
print(math.trunc(34))      # 34
Using negative number
import math
print(math.trunc(-34.11))   # -34
print(math.trunc(-34.99))   # -34
print(math.trunc(-34))      # -34
Working with Large Numbers:
print(math.trunc(123456.789))  # Output: 123456
Using with Variables:
x = 45.67
print(math.trunc(x))  # Output: 45

Difference between trunc(), floor(), and ceil()

  • trunc(): Simply removes the decimal part of the number, returning the integer portion. It does not round the number.
    import math
    print(math.trunc(3.9))  # Output: 3
    print(math.trunc(-3.9))  # Output: -3
  • floor(): Rounds the number down to the nearest integer.
    print(math.floor(3.9))  # Output: 3
    print(math.floor(-3.9))  # Output: -4
  • ceil(): Rounds the number up to the nearest integer.
    print(math.ceil(3.1))  # Output: 4
    print(math.ceil(-3.1))  # Output: -3
Use Cases: Data Processing: Truncate decimals in financial calculations or statistics where fractional parts aren't needed.
floor() & ceil() modf()


Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer