Javatpoint Logo
Javatpoint Logo

Precision Handling in Python

Python provides many built-in methods to handle the precision of floating points. In this tutorial, we will discuss the most common types of methods to set precision in Python. Most of the methods are defined under the math module.

Various Methods to handle Precision

The following methods come with the math module.

  • trunc() - The trunc() method removes all the decimal points from the floating number. It returns the integer value without the decimal part.
  • ceil() - This method prints the least integer greater than the given number.
  • floor() - This method prints the greatest integer smaller than the integer.

Let's understand the following example.

Example -

Output:

The value is: 25
The ceiling value is: 26
The floor value is: 25

Manipulate Decimal Part

In the previous example, we have seen how we can remove the fractional part of a number. Now we will learn how to manipulate the decimal part. First, let's understand the following methods.

  • The % operator - It works as a printf in C and is used to set precision and format. We can customize the limits of precision points to be included in the resultant number.
  • format() - It is a built-in method of Python, which is used to format the string and set precision.
  • round(n,d) - It is used to round off the number n up to d decimal places. We can select the number of digits that can be displayed after the decimal points.

Example -

Output:

The value is: 25.738
The value is: 25.738
The value is: 25.73796

Conclusion

We have discussed the six methods to handle Precision in Python. All the methods are easy to use and return an accurate result.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA