How to round off a floating number using Python?

PythonServer Side ProgrammingProgramming

In this article, we will show you how to round off a floating number 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 –.

  • Pass the input number, decimal value(upto to which the number is to be rounded) as arguments to the round() function to round the input number up to the given numbers of decimals

Example

The following program returns the rounded value of input floating-point numbers up to the specified number of decimals using the round() function–

# rounding the number up to 2 decimal places result_1 = round(4.56782, 2) print("rounding 4.56782 upto 2 decimal places:", result_1) # rounding the number upto 3 decimal places result_2 = round(4.56782, 3) print("rounding 4.56782 upto 3 decimal places:", result_2) # rounding the number to nearest integer result_3 = round(4.56782) print("rounding 4.56782 to nearest integer:", result_3) # rounding the number to ten's place result_4 = round(11.56782, -1) print("rounding 4.56782 to ten's place:", result_4)

Output

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

rounding 4.56782 upto 2 decimal places: 4.57
rounding 4.56782 upto 3 decimal places: 4.568
rounding 4.56782 to nearest integer: 5
rounding 4.56782 to ten's place: 10.0

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 give format of decimal places by passing the input number, format(upto to the decimal places to be rounded) as arguments to it.

Example

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

# input floating-point number inputNumber = 4.56782 # rounding the number up to 3 decimal places print("rounding 4.56782 upto 3 decimal places:", format(inputNumber,".3f")) # rounding the number upto 3 decimal places print("rounding 35.6578 upto 1 decimal place:", format(35.6578,".1f"))

Output

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

rounding 4.56782 upto 3 decimal places: 4.568
rounding 35.6578 upto 1 decimal place: 35.7

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.000’)) to round up to 3 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.

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

  • Round the number upto the specified number digits(here 3 decimal places. hence we give 3 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 given decimal places.

Example

The following program returns the rounded value of the input floating-point number upto the specified number of 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 3 digits after the decimal point roundedNumber = decimalValue.quantize(decimal.Decimal('0.000')) # printing the input floating-point number print("Input floating-point number: ", inputNumber) # printing the rounded value of the input number upto 3 decimal places print("Rounding 35.65782 upto 3 decimal places:", roundedNumber)

Output

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

Input floating-point number: 35.65782
Rounding 35.65782 upto 3 decimal places: 35.658

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 required number 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 input floating-point number upto the specified number of decimals using the ceil() function –

# importing math module import math # input floating-point number inputNumber = 4.56782 # rounding the number upto 3 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 3 decimal places:") print(math.ceil(inputNumber*1000)/1000)

Output

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

Rounding 4.56782 upto 3 decimal places:
4.568

Conclusion

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

raja
Updated on 28-Oct-2022 11:40:35

Advertisements