How to round down to 2 decimals a float using Python?


In this article, we will show you how to round off a floating number upto 2 decimals in python. Below are the various methods to accomplish this task:

  • Using round() function

  • Using format() function

  • Using Decimal Module

  • Using ceil() function

Using round() function

The round() function gives a floating point number with a specified number of decimals which is the rounded version of the specified number.

The function will return the nearest integer because the default value for the number of decimals is 0.

Syntax

round(number, digits)

Parameters

  • number(required)- a number that should be rounded

  • digits(optional)- up to the number of decimals to be rounded. 0 is the default.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Create a variable to store the input number.

  • Pass input number, 2 (decimal value) as arguments to the round() function to round the input number upto the 2 decimal places.

  • Print the rounded value of the input floating-point number upto 2 decimals.

Example

The following program returns the rounded value of the input floating-point number upto the 2 decimals using the round() function–

# input floating-point number inputNumber = 3.367824 # rounding the number up to 2 decimal places roundedNumber = round(inputNumber, 2) # print the rounded value of floating-point number up to 2 decimals print("Rounding 3.367824 upto 2 decimal places:", roundedNumber)

Output

On executing, the above program will generate the following output –

Rounding 3.367824 upto 2 decimal places: 3.37

Using format() function

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Create a variable to store the input floating-point number.

  • Use the format() function (It gives back a formatted version of the input value that has been specified by the format specifier) to round the number upto the 2 decimal places by passing the input number, format (upto to the 2 decimals) as arguments to it.

Example

The following program returns the rounded value of the input floating-point number upto the 2 decimals using the format() function–

# input floating-point number inputNumber = 4.56782 # rounding the number upto 2 decimal places print("Rounding 4.56782 upto 2 decimal places:", format(inputNumber,".2f"))

Output

On executing, the above program will generate the following output –

Rounding 4.56782 upto 2 decimal places: 4.57

Using Decimal Module

Python's decimal module helps to increase the accuracy of floating-point numbers. To use this decimal module, we must import it.

decimal.Decimal(floatnumber) − Gives a decimal value of 50 digits as default.

Here we use value.quantize(decimal.Decimal('0.00')) to round upto 2 digits after the decimal point.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Use the import keyword to import the decimal module.

  • Create a variable to store the input floating-point number.

  • Convert the given floating-point number using the Decimal() function of the decimal module.

  • Round the number upto 2 digits (here 2 decimal palace . hence we give 2 zeros after the decimal point) after the decimal point using the value.quantize(decimal.Decimal()) function.

  • Print the input floating-point number

  • Print the rounded value of the input number upto the 2 decimal places.

Example

The following program returns the rounded value of the input floating-point number upto the 2 decimals using the decimal module –

# importing decimal module import decimal # input floating-point number inputNumber = 35.65782 # Converting the given number to decimal decimalValue = decimal.Decimal(inputNumber) # rounding the number upto 2 digits after the decimal point roundedNumber = decimalValue.quantize(decimal.Decimal('0.00')) # printing the input floating-point number print("Input floating-point number: ", inputNumber) # printing the rounded value of the input number upto 2 decimal places print("Rounding 35.65782 upto 2 decimal places:", roundedNumber)

Output

On executing, the above program will generate the following output –

Input floating-point number: 35.65782
Rounding 35.65782 upto 2 decimal places: 35.66

Using ceil() function

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Use the import keyword to import the math module.

  • Create a variable to store the input floating-point number.

  • Use the ceil() function(returns a ceiling value of the number i.e., the smallest integer greater than or equal to the number), to round the number upto the 2 decimal places and print the resultant number.

If you want to round upto 1, 2, 3, 4...decimals then multiply and divide by
# 10, 100, 1000, 10000 numbers respectively

Example

The following program returns the rounded value of the input floating-point number upto the 2 decimals using the ceil() function –

# importing math module import math # input floating-point number inputNumber = 4.56782 # rounding the number upto 2 decimal places using ceil() function # If you want to round upto 1, 2, 3, 4...decimals then multiply and divide by # 10, 100, 1000, 10000 numbers respectively print("Rounding 4.56782 upto 2 decimal places:") print(math.ceil(inputNumber*100)/100)

Output

On executing, the above program will generate the following output –

Input floating-point number: 35.65782
Rounding 4.56782 upto 2 decimal places:
4.57

Conclusion

In this article, we learned how to round off a given floating number to two decimal places in Python using four different methods. Using the ceil function and some mathematical logic, we learned how to round to two decimal places. We also learned how to use the decimal module to convert a given floating point number to a decimal and quantize it.

Updated on: 29-Aug-2023

142K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements