How to round to 2 decimals in Python

Python round to 2 decimal places

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.

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,

Output:

10.14

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.

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.

Output:

10.14 float

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.

Output:

10.59

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,

Output:

10.14

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.

Output:

Round Down 10.13
Round Up 10.14

That’s all about how to round to 2 decimal places in Python.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *