How to round to two decimals in Python?

This tutorial will learn how to round to two decimals in Python using `round()` and `format()` methods.

Round to two decimals in Python

When you deal with mathematical calculations in Numpy or Python, you will often get a lot of decimal values during the calculation. There are 2 ways to round off the decimals in Python. Let us take a look at both approaches in detail.

Using round() Method

The `round()` method returns the floating-point number, the rounded version of a specified number with the specified number of decimals.

Syntax – round(number, ndigits)

• number – the number to be rounded, can be an integer, decimal/floating.
• ndigits (optional) – number of decimals to use when rounding the number. The default value is 0.

Example – Round to two decimal places

``````# integer round off
print("Integer round off Mid value ",round(7))

# Python code to round off floating number using default value
print("Floating round off  ",round(7.7))

# Python code to round off floating number with mid value
print("Floating round off Mid value ",round(7.5))

# floating number round to two decimal places
distance= 4.3847594892369461
print ("The total distance is ", round(distance,2))``````

Output

``````Integer round off Mid value  7
Floating round off   8
Floating round off Mid value  8
The total distance is  4.38``````

Note: The behavior of round() for floats can be surprising: for example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This is not a bug: it’s a result of the fact that most decimal fractions can’t be represented exactly as a float. See Floating Point Arithmetic: Issues and Limitations for more information.

Using format() method

You can use the `format() `method to handle the precision of the floating-point numbers, and there are many ways to set the precision of the number in Python.

Syntax – string.format(value)

• : introduces the format spec
• 0 enables sign-aware zero-padding for numeric types
• .2 sets the precision to 2
• f displays the number as a fixed-point number

Alternatively, Using “`%`”:- `%`” operator is used to format as well as set precision in Python.

``````# Python code to round to two decimals using format

# initializing value
distance= 4.7287543

# using format() to print value with 2 decimal places
print ("The value of number till 2 decimal place(using format()) is : ",end="")
print ("{0:.2f}".format(distance))

# using "%" to print value with  2 decimal places
print ("The value of number till 2 decimal place(using %) is : ",end="")
print ('%.2f'%distance)

``````

Output

``````The value of number with 2 decimal place(using format()) is : 4.73
The value of number with 2 decimal place(using %) is : 4.73``````

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

TypeError: unhashable type: ‘list’

Table of Contents Hide TypeError: unhashable type: ‘list’Example – unhashable type: ‘list’Solution to TypeError: unhashable type: ‘list’.Solution 1 – By Converting list into a tupleSolution 2 – By Adding list…

[Solved] Python can’t Multiply Sequence by non-int of type ‘str’

Table of Contents Hide TypeError: can’t multiply sequence by non-int of type ‘str’Multiplying string with an integerMultiplying string with another stringSolution TypeError: can’t multiply sequence by non-int of type ‘str’Conclusion…

TypeError: ‘module’ object is not callable

Python throws TypeError: ‘module’ object is not callable when you get confused between the class name and module name. There are several reasons why this might happen while coding. Let’s…

Python Program to Measure the Elapsed Time in Python

There are multiple ways to measure the elapsed time in Python. The modules that are available are time, timeit, and Datetime to measure elapsed time. Using time Module We can use the time module…

Python String index()

Table of Contents Hide SyntaxParametersReturn ValueDifference between index() method and find() methodExample 1: Find the index of a string in PythonExample 2: index() method if the stringExample 3: index() method…

Handle any requests and any extensions in Sitecore

In this article, we will look how to Handle any requests and any extensions in Sitecore without writing any code. With some configuration changes, we could easily allow Sitecore to…