Python print format 2 decimal places

In this example we are going to learn Python program which formats a double value and prints 2 decimal places.

Posted December 16, 2018 at 10:27 AM

Python print format 2 decimal places - Python program to format double and print 2 decimal places

In this Python 3 example we are going to teach you to round double to 2 decimal places.

We will use the format() function to format the data into 2 decimal places. This function is the string function which is used for formatting the values in Python programming language. Let's see the syntax of the of format() function:

Syntax : { } .format(value)

Parameters :
(value) : The parameter can be an integer, floating point numeric constant, string, characters or even variables. The format function will format the data and return the formatted result.

Returntype : The return is the formatted string of the input value.

Today in our example we are formatting double to 2 decimal places and printing on the console.

Here is programming example code:


number=2.365245845

print("Python 3 rounded to 2 decimal places {0:.2f}".format(number))

If run the program it will give following output:


>>> number=2.365245845
>>> 
>>> print("Python 3 rounded to 2 decimal places {0:.2f}".format(number))
Python 3 rounded to 2 decimal places 2.37

Here are more tutorials of Python programming language: