How to Round 2 Decimals with Python?

Rounding To 2 Decimals Python

There can be times at which one would face results with umpteen decimal digits, beyond the comprehensible capabilities of an average human. So, reducing them to something understandable goes a long way in the books of interpreting data. In this guide, we’ll delve into various techniques for rounding numbers to two decimal places in Python.

  • Using the round( ) function
  • Using the format( ) function
  • Using the decimal module
  • Using the ceil( ) function

Let us get started with the very first technique on the list.

In Python, rounding numbers to two decimal places can be done using various methods including the round(), format(), Decimal module, and the ceil() function. The round() function truncates the decimal digits down to 2, the format() function sets the precision of the decimal places, the Decimal module quantizes the number to the desired decimal places, and the ceil() function from the math module rounds the decimals up to the nearest integer.


Method 1: The round( ) Function in Action

As the name indicates, the round( ) function will return a floating point number to a specified number of decimals. Following is its syntax.

round(number, digits)

In the below example, we shall create a variable for storing the input number with decimal places. Then this variable shall be put through the round( ) function so that it truncates the decimal digits down to 2. Let us have a look at how it is done.

ip = 2.1258908
op = round(ip,2)
print("Rounded number:", op)

The output for this code will be 2.13. Here, the round() function is used to round the number to two decimal places.

Input Rounded To 2 Decimals Using Round
Input rounded to 2 decimals using round( )

Method 2: The format( ) Function Demystified

Now let us get on with another technique that can be used to round the decimal places to 2. The format( ) function makes use of the format specification operand ‘:’ to set the precision of the decimal places. Let us have a look at how it is done.

ip = 2.1258908
op = format(ip,".2f")
print("Rounded number:", op)
Rounded number: 2.13
Input Rounded To 2 Decimals Using Format
Input rounded to 2 decimals using format( )

Method 3: Using the Decimal Module

Rather than a function, this time it is a module which ought to be imported from the Python library using the following code.

import decimal

By default, this module will output a decimal value with 50 digits after the decimal point. However, one can modify this to one’s requirement. This can be done by using the quantize( ) function where the number of digits to which the decimal is to be rounded is given in the form of zeros as shown below.

quantize(decimal.Decimal('0.00')

Now it is time to witness the functioning of the decimal module and the nuances of using it to round the decimal digits to two. We shall start things by running the floating point number through the decimal( ) function.

import decimal
ip = 2.1258908
dec = decimal.Decimal(ip)

Then the given number is run through the quantize( ) function to round the decimals to two digits as shown below.

op = dec.quantize(decimal.Decimal('0.00'))
Rounding To 2 Decimals By Decimal Module
Rounding to 2 decimals by decimal module

Method 4: Exploring the ceil( ) Function

The ceil( ) function is the final technique that will be covered in this article detailing the different ways to round to 2 decimals. This function rounds the decimals up to the nearest integer of the given input number. To make use of this function, one has to import its repository – the math module. Let us get things started by importing the math module using the below code.

import math

Once done, one can run the input value through the ceil( ) function & round it to 2 decimal places by multiplying & dividing the value by 100 as shown below.

ip = 2.1258908
op = math.ceil(ip*100)/100
Rounding To 2 Decimals By Ceil
Rounding to 2 decimals by ceil( )

Note – This function rounds the number up, not to the nearest number

Conclusion:

Now that we have reached the end of this article, hope it has elaborated on the different techniques that can be used to round to 2 decimal places in Python. Here is another article that details how to round to the nearest integer using the numpy fix( ) function in Python. There are numerous other enjoyable and equally informative articles in AskPython that might be of great help to those who are looking to level up in Python. As you continue to hone your Python skills, remember that ‘Audere est facere’ – ‘To dare is to do’. What new Python challenges will you dare to tackle next?

What new Python challenges will you dare to tackle next?


Reference: