There are multiple modules and libraries supported by Python to perform mathematical computation. For instance, modules like “numpy”, “pandas”, “math”, etc., are used to solve simple to complex mathematical problems. Rounding a number is a mathematical problem that can be resolved using various built-in functions or modules such as the round() function, format() function, Decimal module, etc.
This write-up will provide you with various methods that are used to round a float to 2 decimals in Python. The aspects discussed in this article are the following:
- Method 1: Using round() Function
- Method 2: Using ceil() Function
- Method 3: Using format() Function
- Method 4: Using f-string Method
- Method 5: Using Decimal Module
Method 1: Using round() Function
The “round()” function is utilized to convert the float number into the nearest number according to the specified decimal value. The “round()” function accepts two argument values: the input number and a ‘digit’ to determine the number of decimal places(optional). Let’s understand it via the following code:
Code:
round_num = round(7.69827, 2)
print("Rounding Input Number:", round_num)
In the above code, the “round()” function takes two argument values: the input number “7.69827” and a ‘digit’ to determine the number of decimal places “2”. Consequently, it will return a float value with 2 decimals.
Output:
The above output verifies that the input number has been rounded to 2 decimals.
Method 2: Using ceil() Function
In the example given below, the “ceil()” function is used to get the ceiling value which will be greater than or equal to the input number. The user-defined logic is used along with the “ceil()” function to get the accurate round float value to 2 decimal places. Here is an example:
Code:
import math
num = 9.6763
print(math.ceil(num*100)/100)
In the above program, the “math” module is imported at the beginning of the program that will be used to access the “math.ceil()” function. To round the given number to 2 decimal places, firstly, the given number will be multiplied by “100”. Afterward, pass that number to the “ceil()” function. Finally, divide the return value of the ceil() function by 100 to round the input number up to “2” decimals.
Output:
The above output shows that the input number has been rounded to 2 decimals using the “math.ceil()” method.
Method 3: Using format() Function
In the example given below, the “format()” function is utilized to round a float value to 2 decimals. This method uses specific format specifiers to fix the fractional value of input numbers. Let’s understand it via the following code:
Code:
num = 9.6567
print(format(num ,".2f"))
In the above code, the “format()” function accepts the float variable “num” and the format specifier “.2f” to round a float to 2 decimals places.
Output:
The above output verified that the input number had been rounded to 2 decimals using the “format()” method.
Method 4: Using f-string Method
The “f-string” method is very similar to the “format()” method, which uses specified specifiers to round an input floating point value to 2 decimals. The examples of this are shown below:
Code:
num = 6.48975
round_num = float(f'{num:.2f}')
print(round_num)
In the above code, the float variable “num” is placed inside the “curly braces” along with specified specifier expression. The specified expression “.2f” rounds the input float number up to 2 decimals. The “float()” function takes the string rounded value as an argument and casts it into the float.
Output:
The above output shows that the input float number has been successfully rounded to 2 decimals.
Method 5: Using Decimal Module
The “decimal” module function “Decimal()” and “quantize()” is used to round the input float number up to “2” decimals. Here is an example:
Code:
import decimal
num = 7.64767
dec_value = decimal.Decimal(num)
round_num = dec_value.quantize(decimal.Decimal('0.00'))
print(round_num)
In the above Python program, the “decimal” module is imported. The “decimal.Decimal()” function is used to convert the input float number into a “50” digits decimal number. The “quantize(decimal.Decimal())” function with the parameter value “0.00” is utilized to convert the floating decimal up to 2 digits.
Output:
The input number has been rounded to 2 decimals successfully.
Conclusion
To round a float to “2” decimals, the “round()”, “math.ceil()”, “format()”, “f-string”, and “decimal.Decimal()” functions are used in Python. The “round()” function takes the float value and specified decimal value as an argument and returns the rounded value. The “format()” method and “f-string” method used the format specifiers to round a float value to “2” decimals. This Python article presented a detailed guide on how to round a float to 2 decimals in Python.