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,



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.


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.



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,



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.


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?


You may also like:

Related Posts

  • 29 November

    Prefix b Before String in Python

    Table of ContentsPrefix b Before String in PythonConclusion Prefix b Before String in Python Prefix b before String denotes a byte String. By putting b before String, you can convert String to bytes in Python. The upgrade from Python 2 to Python 3 was considered a major change as many new features were introduced and […]

  • 29 November

    How to Log to Stdout in Python

    Table of ContentsUse logging.basicConfig() to log to stdoutUse logging.StreamHandler() to log to stdout Use logging.basicConfig() to log to stdout To log to stdout in Python: Specify the format in which we want to have all logs. Use the helper function .basicConfig() to perform basic logging for the logging system. Use .getLogger() to create a logger […]

  • 29 November

    Skip Iterations in Python loop

    Table of ContentsSkip iterations in a Python loop.Using the continue statement.Special Case: Using exception handling. 💡TL;DR To skip iterations in Python loop, use continue statement. [crayon-638b53daf18e6093505454/] Looping is one of the fundamentals in Python with one or more types of loops occurring in almost every code. The for loop and the while loop allow the […]

  • 28 November

    Convert String to Path in Python

    Table of ContentsUsing pathlib library [Python 3.4+]Using os.path moduleConclusion Using pathlib library [Python 3.4+] Use Path class’s constructor to convert String to Path in Python. You need to import Path class from pathlib. [crayon-638b53daf1df8441647921/] Output: C:\temp\tempFile.txt We used the Path() constructor to convert String to Path. This constructor accepts a string that contains a required […]

  • 28 November

    Get String Between Two Characters in Python

    Table of ContentsUsing the string-slicing techniqueUsing Regular ExpressionsUsing the split() functionConclusion Using the string-slicing technique To get String between two characters in Python: Use String’s find() method to find indices of both the characters. Use String’s slicing to get String between indices of the two characters. [crayon-638b53daf22b3508268040/] Output: ava2Blo In the above example, we found […]

  • 28 November

    Check if Date is Greater than Today in Python

    Table of ContentsUse Comparison Operator with now()Compare the datetime of Different Time ZonesUse Comparison Operator with strptime() Use Comparison Operator with now() To check if the specified date is greater than today in Python: Use to get the current local time and date. Create a datetime object using datetime.datetime() with the specified date and […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.