Several math methods in Python help users perform different mathematical operations like exponent, rounding, square root, etc. In this article, you will learn how to round a floating point number to two decimal places. These techniques include :
What is a decimal place?
A decimal place signifies the position right to the decimal point in the number line. Users can specify a value such as tenths, hundredths, etc. place values based on the position of the digits. A number contains two parts, a whole number part, and a fractional part. The place value of :
- three is hundreds
- two is tens
- four is ones
- eight is tenths
- nine is hundredths
The above list indicates: there are two numbers, i.e., eight, then nine after the decimal point. Thus, the decimal place of eight (i.e., in the tenth place) is 1, and nine (i.e., in the hundredth place) is 2.
How to round up to a specific decimal place?
Rounding decimals indicates rounding a number's decimal digits to a specific degree of accuracy. Users can round decimals to the nearest wholes, tenths, or hundredths places. Thus, users will round up a number to a decimal place based on the succeeding number.
It means if a number after the decimal place is five or more, they can round that number at the decimal place up to +1. Else, the number at the decimal place remains the same. Or, users can round the number after the decimal place down to 0.
Let us suppose we have two decimal numbers, 234.76 and 42.74. Next, we round these decimals to one decimal place or the nearest tenth. So, the first number 234.76 has 7 in the one decimal place, and 6 is the second decimal place, which is more than 5. Thus, we will round up the one decimal place number to 8, i.e., 234.8 (nearest tenth).
Again, in the second number 42.74, since 4 is not larger than 5, 7 remains the same and we will round the second decimal place down to 0. It will produce 42.7 after rounding the number to one decimal place. Thus, based on how many places users want to keep the digits of a number after the decimal places, they can specify using the following techniques in Python:
Method 1: Using the round() function
The round() function is a built-in Python function that allows users to round a floating point number to the provided decimal place, given as input.
Syntax:
round(number, number of digits)
It accepts two parameters :
- number: This parameter specifies the number which users want to round
- number of digits: Users can use this parameter to specify the number of digits up to which they want to round the assigned number.
Code Snippet:
a = 251.7362719
res = round(a, 2)
print("Rounding", a, "upto 2 decimal places:", res)
Output:
Explanation:
In the above example, we rounded the floating point number to two decimal places using the round() function. We passed the number in the assigned variable and gave two (decimal value) as the parameter into the Python function. It then rounds the number to two decimal places.
Method 2: Using the ceil() function
Users can use the ceil() function in Python to return a ceiling value of a number (floating point or integer). It indicates the smallest integer greater than or equal to that number.
Syntax:
math.ceil(a)
Code Snippet:
import math
a = 462.75861
print("Round the number 462.75861 upto two decimal places: ")
print(math.ceil(a*100)/100)
Output:
Explanation:
We imported the math module to use the ceil() function. Then, we initialized a variable to assign a floating-point number. The ceil() function returns a ceiling value of the number 462.75861. It rounds the number to two decimal places.
Method 3: Using Decimal Module
In Python, the Decimal Module allows users to carry out high-speed double-precision floating point numbers. Users can use this module to handle decimal points or floating-type numbers, which increases the accuracy of floating-point numbers.
The Decimal quantize() method is a decimal class method that returns a value the same as the first decimal value (rounded) with the exponent of the second decimal value.
Syntax:
Decimal.quantize()
Parameters used:
It takes decimal values as the parameters.
Code Snippet:
import decimal
a = 8481.578319
b = decimal.Decimal(a)
res = b.quantize(decimal.Decimal('0.00'))
print("The assigned floating-point number is: ", a)
print("After rounding the number", a, "upto two decimal places, it produces:", res)
Output:
Explanation:
First, we imported the Decimal Module to use the quantize() method. Then, we assigned a floating-point number to a variable. The decimal() function of the Decimal Module converts the floating-point number.
Then, we used the value.quantize(decimal.Decimal()) function that rounds the number to two decimal places. For this, we passed two zeros after the decimal point, which specifies the exact precision.
The quantize() method returns a number after rounding the decimal numbers to specified places.
Method 4: Using the format() method
Formatters work by formatting a specified value.
Syntax:
string.format(value1, value2...)
It takes more than two parameters which are specified formatted values.
Code Snippet:
a = 734.4898
print("We round the floating-point number", a, "upto two decimal places and got:", format(a,".2f"))
Output:
Explanation:
Here, we used the format() method to return the formatted version of the given value, which we specified by the format specifier. It rounds the number up to two decimal places, as given in the second parameter.
Conclusion
Rounding floating-point numbers to two decimal places is as easy as we can see with the above Python methods. This article provides four techniques for rounding a floating number to the specified precision value.
Before knowing how to round a floating-point number, users should know what decimal places mean, which is there in the first section of the article.