In Python, we deal with integers, floating values, and even imaginary numbers.
Float numbers are used to represent the real numbers with integers and decimal parts. Python represents such numbers with 64-bit precision giving it a range of almost 1.8 x 10^308
.
In this article, we will discuss how to round float values to 2 decimal places.
The methods discussed here can round up float values to our desired decimal places. However, our examples will round them to 2 decimal places.
Table of Contents
Using the round()
function
The round function is used traditionally to round up numbers to the specified decimal places. We can specify the total digits required after the decimal point in the function itself.
For example,
1 2 3 4 |
a = 10.1364 print(round(a,2)) |
Output:
In the above code, we rounded a floating number to two decimal places. If we need to round the number to 2 decimal places and the number has less than 2 decimal places then the number itself gets returned.
Further reading:
Using the format()
function
We can format strings in Python using many methods to get the final result in our desired format. The format()
function is one such way in which we can achieve this.
We can round up numbers using this function by specifying the string format as {.2f}
. In this format, the f is used to indicate a floating value, and .2 specifies that we require the value to have 2 decimal places.
See the following code on how to use this method.
1 2 3 4 5 |
a = 10.1364 s = float("{:.2f}".format(a)) print(s, type(s)) |
Output:
Note that after using this function, the final result is changed to string (str) type. We typecast this and get the final result in float type by using the float()
function.
Using the %
formatting
Another advantage of using string formats is that we can use them without the format()
function in the print()
function also.
So if we want to just print the value with two decimal places we can do it as shown below.
1 2 3 4 5 |
def print_rounded(answer): print (" %.2f " % answer) print_rounded(10.5894) |
Output:
Using the f-strings
The f-strings
is a new addition in Python 3. It was added as a faster alternative to the %
and format()
function to format strings. It provides a concise way to edit and format strings.
We just have to specify the format and prefix the expression with ‘f’ to achieve our desired format. We can use this method to round float values to required decimal places.
For example,
1 2 3 4 5 |
a = 10.1364 s = float(f"{a:.2f}") print(s) |
Output:
Note the use of the float()
function to get the final result in float type.
Using the math.ceil()
and math.floor()
functions
The math module is used for working with and evaluating mathematical expressions and constants. The ceil()
function from this module is used to return the smallest integer which is greater or equal to the number passed to this function. The floor()
returns the greatest number which is smaller or equal to the number passed to this function.
These two functions cannot be used entirely on their own to solve our problem. However, we can use them to create a small user-defined function which will either round up or round down a number to two decimal places.
We need to use one of these functions as per our requirements. If we use the floor()
function then the final result will get rounded down. It will get rounded up with the ceil()
function.
We will implement the logic for two user-defined functions to round decimal places in the following code snippet.
1 2 3 4 5 6 7 8 9 10 11 |
import math def round_down(number, decimals = 2): f = 10 ** decimals return math.floor(number * f) / f def round_up(number, decimals = 2): f = 10 ** decimals return math.ceil(number * f) / f print("Round Down", round_down(10.1364)) print("Round Up", round_up(10.1364)) |
Output:
Round Up 10.14
That’s all about how to round to 2 decimal places in Python.