Tutorial Example

Format Python Float to 2 Decimal Places: A Step Guide – Python Tutorial

In python, we can use built-in function round() to format a float number to two decimal places. In this tutorial, we will use some examples to show you how to do.

Python round()

round(number[, ndigits])

Return number rounded to ndigits precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input.

For example:

n = 2.3123

x1 = round(n)
x2 = round(n, 1)
x3 = round(n, 2)
x4 = round(n, 3)
print(x1, x2, x3, x4)

Run this code, you can find: