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?


import_contacts

You may also like:

Related Posts

  • 25 June

    Convert Roman Number to Integer in Python

    Table of ContentsHow to Convert Roman Number to Integer in PythonHow to convert roman number to integer in python?Using the if...else statement to convert roman number to integer in Python.Using classes and a dictionary to convert roman number to integer in Python.Using the roman module to convert roman number to integer in Python.ConclusionWas this post […]

  • 11 June

    Remove Single Quotes from String in Python

    Table of ContentsRemove single quotes from string in Python.How to remove single quotes from string in Python?Using the replace() function to remove single quotes from string in Python.Using a for loop to remove single quotes from string in Python.Using the join() function to remove single quotes from string in Python.Using the re module functions to […]

  • 11 June

    Escape Backslash Character in Python

    Table of ContentsEscape backslash character in python.How to use escape backslash character in python?Using the escape backslash character in python to represent whitespace characters.Using the escape backslash character in python to turn special characters into ordinary characters.Using the escape backslash character in f-strings in Python.Using the escape backslash character in raw strings in Python.Was this […]

  • 11 June

    Break out of function in Python

    Table of ContentsBreak out of function in PythonHow to break out of function in Python?Using the return statement to break out of function in PythonUsing the try and except block to break out of function in PythonUsing the sys.exit() function to break out of function in PythonConclusionWas this post helpful? In this post, we will […]

  • 11 June

    Check if Object Is Iterable in Python

    Table of ContentsWhat are iterables in Python?How to check if object is iterable in Python?Using the iter() function to check if object is iterable in PythonUsing the for loop to check if object is iterable in PythonUsing the isinstance() function to check if object is iterable in PythonConclusionWas this post helpful? In this post, we […]

  • 11 June

    Save Numpy Array as Image in Python

    Table of ContentsSave numpy array as image in PythonHow to save numpy array as image in Python?Using the PIL library to save numpy array as image in PythonUsing the matplotlib.image library to save numpy array as image in PythonUsing the opencv library to save numpy array as image in PythonUsing the imageio library to save […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.