# How to round to 2 decimals in Python 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.

## Related Posts

• 15 July

### Generate random number between 0 and 1 in Python

Table of ContentsUsing the random.uniform() function.Using the random.random() functionUsing the random.randint() functionUsing the numpy.random.random() functionUsing the numpy.random.uniform() function Generating Random numbers is easily achievable in Python, as Python provides the Random module that contains some functions that can be used to generate random numbers as per the programmer’s needs. A Random number does not generate […]

• 20 June

### Convert String to Boolean in Python

Table of ContentsUsing the bool() method to convert string to Boolean in Python.Using the distutils.util.strtobool() method to convert a string to Boolean in Python.Using list comprehension to convert a string to Boolean in Python.Using the map() function along with Lambda function to convert a string to Boolean in Python.Using the JSON parser to convert a […]

• 19 June

### Convert float to int in Python

Table of ContentsUsing the int() function to convert float to int in PythonUsing the math module functions to convert float to int in PythonUsing ceil()Using trunc()Using floor Python provides several in-built functions and modules for datatype conversions. Floating-point numbers can be easily converted to integers using some of these in-built functions. This tutorial will discuss […]

• 12 June

### Detect keypress in Python

Table of ContentsUsing the keyboard module to detect keypress in PythonUsing the pynput module to detect keypress in PythonUsing the msvcrt module to detect keypress in Python Python allows us to work with user input in its programs. We can also work with hardware devices in Python. In this article, we will discuss how to […]

• 12 June

### 7 ways to copy a dictionary in python

Table of ContentsUsing the copy() function to copy a dictionary in PythonUsing the deepcopy() function to copy a dictionary in PythonUsing the dict() function to copy a dictionary in PythonUsing the unpack operator ** to copy a dictionary in PythonUsing the update() function to copy a dictionary in PythonUsing the dictionary comprehension method to copy […]

• 11 June

### Escape quotes in Python

Table of ContentsUsing \ to escape quotes in PythonUsing \ with replace() to escape quotes in PythonUsing json.dumps to escape quotes in PythonUsing triple single or double quotes to escape quotes in pythonUse r to include \ and quotes in String 💡 Outline You can use \ to escape quotes in Python. If you want […]

## Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.