 # Program to Handle Precision Values in Python

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

Python treats every number with a decimal point as a double precision floating point number by default. The Decimal is a form of floating decimal point with more precision and a narrower range than the float. It is ideal for monetary and financial calculations. It is also more similar to how humans operate with numbers.

In contrast to hardware-based binary floating point, the decimal module features user-adjustable precision that can be as large as required for a given situation. The precision is set to 28 places by default.

Some values cannot be represented exactly in a float data format. For example, saving the 0.1 value in the float (binary floating point value) variable gives just an approximation of the value. Similarly, the 1/5 number cannot be precisely expressed in decimal floating point format.
Neither type is ideal in general, decimal types are better suited for financial and monetary computations, while double/float types are better suited for scientific calculations.

Handling precision values in Python:

We frequently encounter circumstances in which we process integer or numeric data into any application or manipulation procedures, regardless of programming language. We find data with decimal values using the same procedure. This is when we must deal with accuracy values.

Python has a number of functions for dealing with accuracy values for numeric data. It allows us to exclude decimal points or have customized values based on the position of the decimal values in the number.

Examples:

Example1:

Input:

given_number = 2345.1347216482926    precisionlimit=4

Output:

the given number upto 4 decimal places 2345.1347

Example2:

Input:

given_number = 2345.13    precisionlimit=4

Output:

the given number upto 4 decimal places 2345.1300

## Code for Handling Precision Values in Python

There are several ways to handle the precision values in python some of them are:

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

### Method #1:Using % operator

We may format the number as well as specify precision boundaries with the ‘ % ‘ operator. This allows us to customize the precise point restrictions that are included in the final number.

Syntax:

'%.point'%number

Parameters:

point: It denotes the number of points we want after the decimal in the integer.
number : The integer value to be worked on is represented by the number.

Below is the implementation:

# given number
given_number = 2345.1347216482926
# using % operator and printing upto 4 decimal places
ans = '%.4f' % given_number
print("the given number upto 4 decimal places", ans)


Output:

the given number upto 4 decimal places 2345.1347

### Method #2:Using format function

We can use the format() function to set limits for precision values, just like we can with the percent operator. We format the data as a string and set the limits for the points to be included after the decimal portion of the number with the format() function.

Syntax:

print ("{0:.pointf}".format(number))

Below is the implementation:

# given number
given_number = 2345.1336327777377
# using % operator and printing upto 5 decimal places
ans = '{0:.5f}' .format(given_number)
print("the given number upto 5 decimal places", ans)


Output:

the given number upto 5 decimal places 2345.13363

### Method #3:Using round() function

We can extract and display integer values in a customized format using the Python round() function. As a check for precision handling, we can select the number of digits to be displayed after the decimal point.

Syntax:

round(number, point)

Below is the implementation:

# given number
given_number = 2345.1336327777377
# using % operator and printing upto 4 decimal places
ans = round(given_number, 5)
print("the given number upto 5 decimal places", ans)


Output:

the given number upto 5 decimal places 2345.13363

### 4)Math functions in Python to handle precision values

Aside from the functions listed above, Python also provides us with a math module that contains a set of functions for dealing with precision values.

The Python math module includes the following functions for dealing with precision values–

1)trunc() function
2)The ceil() and floor() functions in Python
Let us go over them one by one.

### Method #4:Using trunc() function

The trunc() function terminates all digits after the decimal points. That is, it only returns the digits preceding the decimal point.

Syntax:

import math
math.trunc(given number)

Below is the implementation:

import math
# given number
given_number = 39245.1336327777377
# truncating all the digits of given number
truncnumber = math.trunc(given_number)
print("the given number after truncating digits", truncnumber)


Output:

the given number after truncating digits 39245

### Method #5:Using ceil() and floor() functions

We can round off decimal numbers to the nearest high or low value using the ceil() and floor() functions.

The ceil() function takes the decimal number and rounds it up to the next large number after it. The floor() function, on the other hand, rounds the value to the next lowest value before it.

Below is the implementation:

import math
given_number = 3246.3421

# prining the ceil value of the given number
print('printing ceil value of the given numnber',
given_number, '=', math.ceil(given_number))

# prining the floor value of the given number
print('printing floor value of the given numnber',
given_number, '=', math.floor(given_number))


Output:

printing ceil value of the given numnber 3246.3421 = 3247
printing floor value of the given numnber 3246.3421 = 3246

Related Programs: