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

By | August 10, 2021

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:

  • ndigits = None, x1 = 2
  • ndigits = 1, x1 = 2.3
  • ndigits = 2, x1 = 2.32 (2 decimal places)
  • ndigits = 3, x1 = 2.312 (3 decimal places)

Leave a Reply