# Precision Handling in Python

## Precision Handling in Python

Python in its definition allows handling the precision of floating-point numbers in several ways using different functions. Most of them are defined under the “math” module. Some of the most used operations are discussed in this article.

1. trunc():- This function is used to eliminate all decimal parts of the floating-point number and return the integer without the decimal part.
2.  ceil():- This function is used to print the least integer greater than the given number.
3. floor():- This function is used to print the greatest integer smaller than the given integer.
 `# Python code to demonstrate ceil(), trunc()` `# and floor()` `# importing "math" for precision function` `import` `math` `# initializing value` `a ``=` `3.4536` `# using trunc() to print integer after truncating` `print``(``"The integral value of number is : "``, end``=``"")` `print``(math.trunc(a))` `# using ceil() to print number after ceiling` `print``(``"The smallest integer greater than number is : "``, end``=``"")` `print``(math.ceil(a))` `# using floor() to print number after flooring` `print``(``"The greatest integer smaller than number is : "``, end``=``"")` `print``(math.floor(a))`

Output :

```The integral value of number is : 3
The smallest integer greater than number is : 4
The greatest integer smaller than number is : 3```

Setting Precision

There are many ways to set the precision of the floating-point values. Some of them are discussed below.

1. Using “%”:- “%” operator is used to format as well as set precision in python. This is similar to “printf” statement in C programming.
2.  Using format():- This is yet another way to format the string for setting precision.
3.  Using round(x,n):- This function takes 2 arguments, number, and the number till which we want decimal part rounded.
 `# Python code to demonstrate precision` `# and round()` `# initializing value` `a ``=` `3.4536` `# using "%" to print value till 2 decimal places` `print` `(``"The value of number till 2 decimal place(using %) is : "``,end``=``"")` `print` `(``'%.2f'``%``a)` `# using format() to print value till 2 decimal places` `print` `(``"The value of number till 2 decimal place(using format()) is : "``,end``=``"")` `print` `(``"{0:.2f}"``.``format``(a))` `# using round() to print value till 2 decimal places` `print` `(``"The value of number till 2 decimal place(using round()) is : "``,end``=``"")` `print` `(``round``(a,``2``))`

Output :

```The value of number till 2 decimal place(using %) is : 3.45
The value of number till 2 decimal place(using format()) is : 3.45
The value of number till 2 decimal place(using round()) is : 3.45```

Last Updated on October 25, 2021 by admin

## How to get column names in Pandas dataframeHow to get column names in Pandas dataframe

While analyzing the real datasets which are often very huge in size, we might need

## Python String isalpha() MethodPython String isalpha() Method

Python String isalpha() Method Python String isalpha() method is a built-in method used for string handling. The

## __name__ (A Special variable) in Python__name__ (A Special variable) in Python

__name__ (A Special variable) in Python Since there is no main() function in Python, when

## How to Install Python Pandas on Windows and Linux?How to Install Python Pandas on Windows and Linux?

Install Python Pandas on Windows and Linux   Pandas in Python is a package that

## Python program to print EmojisPython program to print Emojis

Python program to print Emojis There are multiple ways we can print the Emojis in

## How to Convert Floats to Strings in Pandas DataFrame?How to Convert Floats to Strings in Pandas DataFrame?

Convert Floats to Strings in Pandas DataFrame In this post, we’ll see different ways to

## Python – yield KeywordPython – yield Keyword

Python | yield Keyword Yield is a keyword in Python that is used to return from

## Python PIL – Image.resize() methodPython PIL – Image.resize() method

Python PIL | Image.resize() method PIL is the Python Imaging Library which provides the python