🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The trunc function in Python's math module is used to truncate a given number, effectively removing its fractional part and returning the integer part. This function is essential in various fields such as mathematics, data analysis, computer science, and financial calculations where operations involving truncation are required.
Table of Contents
- Introduction
- Importing the
mathModule truncFunction Syntax- Examples
- Basic Usage
- Handling Negative Numbers
- Handling Edge Cases
- Real-World Use Case
- Conclusion
- Reference
Introduction
The trunc function in Python's math module allows you to truncate a given floating-point number, removing its fractional part and returning the integer part. This is particularly useful when you need the integer portion of a number without rounding.
Importing the math Module
Before using the trunc function, you need to import the math module.
import math
trunc Function Syntax
The syntax for the trunc function is as follows:
math.trunc(x)
Parameters:
x: A numeric value.
Returns:
- The truncated integer part of
x.
Examples
Basic Usage
To demonstrate the basic usage of trunc, we will truncate a few floating-point numbers.
Example
import math
# Truncate 3.5
result = math.trunc(3.5)
print(result) # Output: 3
# Truncate 7.9
result = math.trunc(7.9)
print(result) # Output: 7
# Truncate 0.1
result = math.trunc(0.1)
print(result) # Output: 0
Output:
3
7
0
Handling Negative Numbers
This example demonstrates how trunc handles negative numbers by truncating their fractional part and returning the integer part.
Example
import math
# Truncate -3.5
result = math.trunc(-3.5)
print(result) # Output: -3
# Truncate -7.9
result = math.trunc(-7.9)
print(result) # Output: -7
Output:
-3
-7
Handling Edge Cases
This example demonstrates how trunc handles special cases such as zero and very large numbers.
Example
import math
# Truncate 0.0
result = math.trunc(0.0)
print(result) # Output: 0
# Truncate a very large number
large_value = 1e10 + 0.5
result = math.trunc(large_value)
print(f"Truncated large number: {result}") # Output: 10000000000
# Truncate a very small number
small_value = 1e-10
result = math.trunc(small_value)
print(f"Truncated small number: {result}") # Output: 0
Output:
0
Truncated large number: 10000000000
Truncated small number: 0
Real-World Use Case
Financial Calculations: Removing Fractional Cents
In financial calculations, the trunc function can be used to remove fractional cents from monetary values, ensuring that only whole cents are considered.
Example
import math
# Function to truncate monetary value
def truncate_cents(value):
return math.trunc(value * 100) / 100
# Monetary values
values = [123.456, 78.910, 50.12345]
# Truncating fractional cents
truncated_values = [truncate_cents(value) for value in values]
print(f"Truncated monetary values: {truncated_values}")
Output:
Truncated monetary values: [123.45, 78.91, 50.12]
Conclusion
The trunc function in Python's math module is used for truncating a given number to its integer part. This function is useful in various numerical and data processing applications, particularly those involving truncation in fields like mathematics, data analysis, and financial calculations. Proper usage of this function can enhance the accuracy and efficiency of your computations.
Reference
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment