10 ways to round to 2 decimal places in Python

Python PythonBasic

In this post, we are going to understand a different 10 ways to round to 2 decimal places in Python by using the python built-in round() function, str. format(),f-string, and all possible ways to round Float to 2 decimal places python

What is Python round() function


The Python’s round() function rounds off a given number based on the numbers of digits passed for round off and returns a floating-point number. If the number of digits for round-off is not specified, the default value will be 0 and it returns the nearest integer.

Syntax

round(number,digits)

Parameters

  • number: The number that we have to round off.
  • digits: The number of digits up to the number to be round off.

1.Round to 2 decimal Places


In this example, we are using Python’s inbuilt round() function to round off a float number to 2 decimal places and using the print() function to print the result.

Program Exmaple

float_num = 15.68790
rounded_num = round(float_num,2)
print('Round to 2 decimal Places :',rounded_num)  

Output

Round to 2 decimal Places : 15.69

2. Round to different decimal places


In this example we are using the Python round() function to round off a float number to (5,3,4,2) decimal points and Finally, using print() function to print the result.

Program Example

val1 = 12.505160
val2 = 1645.7409947
val3 = -200.75
val4 = 8.5432


print('rounded to 5 decimal place:',round(val1, 5))
print('rounded to 2 decimal place:',round(val2, 2))
print('rounded to 3 decimal place:',round(val3, 3))
print('rounded to 4 decimal place:',round(val4, 4))


Output

rounded to 5 decimal place: 12.50516
rounded to 2 decimal place: 1645.74
rounded to 3 decimal place: -200.75
rounded to 4 decimal place: 8.5432

3. str.format() to round off to 2 Decimal Places


To round off up to 2 decimal places we can use the inbuilt string function str.format() with “{:2f}” a string and pass a float number.

Program Example

float_num = 5.5667
two_dec_places  = "{:.2f}".format(float_num)
print('float to two decimal palace:\n',two_dec_places)



Output

float to two decimal palace:
 5.57

4. f-string to Round off to 2 Decimal Places


In this example, we are using the f-string to round off a number to 2 decimal palaces.

Program Exmaple

float_num = 5.8967


rounded_num = f"{float_num:.2f}"



print('rounded upto 2 decimal place :\n',rounded_num)

Output

rounded upto 2 decimal place :
 5.90

5. Using decimal Module


To get the correct precision, we can use the decimal module decimal() method of Python. Let us understand with an example, how to achieve this

Program Example

import decimal

original_num = 8.5656789

float_num = decimal.Decimal(original_num)

print('precision to 2 decimal place:',round(float_num,2)
)

Output

precision to 2 decimal places: 8.57

6. Regex to Truncate float Number to 2 Decimal Places


In this example, we are using the regular expression to Truncate floating Numbers to 2 Decimal Places, by defining a custom method. Here we are passing the number which we want to truncate to 2 decimal places and print the result after truncation.

Program Example

import re


def truncate_num(float_num):
    return re.sub(r'^(\d+\.\d{,2})\d*$',r'\1',str(float_num))


print('rounded number to 2 decimal place :\n',truncate_num(float_num))

Output

rounded number to 2 decimal place :
 5.89

7. Custom function to Truncate float to 2 decimal places


In this example, we are using a custom truncate function that takes two arguments as float numbers & the number of digits up to where we have to truncate the number. Here we are truncating float numbers to 2 decimal places.

Program Example

float_num = 5.8967

def truncatenum(num,digits=0):
    multiplier = 10 ** digits
    return int(num * multiplier) / multiplier

print('rounded number to 2 decimal place :\n',truncatenum(float_num,2))


Output

rounded number to 2 decimal place :
 5.89

8. Round a float number keeping ending zero


In this example, we are rounding off a float number to 2 decimal palaces keeping the ending zero and print the result using the print() function.

Program Example

float_num = 5.8967

round_keep_zero  = "%.2f" % float_num


print('round_keep_zero:\n',round_keep_zero)

Output

round_keep_zero:
 5.90

9. Add extra zero after decimal in Python


In this example, we are adding extra zero after decimal places by using the format() method.

Program Example

float_num = 5.0
output_num = format(2.0, '.6f')
print('add extra zero after decimal place :\n',output_num)


Output

add extra zero after decimal place :
 2.000000

10.Math module for Python Handling precision


The python math module has some built-in functionality to handle the precision.

  • trunc():-This math module function is used to remove all the decimal parts from the float number and return an integer part of a number.
  • ceil(): The ceil() math module is used to return the smallest integer number greater than the given number.
  • floor(): The floor() is used to return the greatest integer number smaller than the given number.

Program Example

import math

float_num = 5.8967

print('number after using trunc() function :',math.trunc(float_num))

print('number after using ceil() function : ',math.ceil(float_num))

print('number after using floor() function : ',math.floor(float_num))

Output

number after using trunc() function : 5
number after using ceil() function :  6
number after using floor() function :  5