Python Format Number to 2 Decimal Places Example

AuthorSumit Dey Sarkar

Pubish Date22 Apr 2023

categoryPython

In this tutorial we will learn Python Format Number to 2 Decimal Places Example.

 

Python Format Number to 2 Decimal Places Example

You can use the format() method in Python to format a number to 2 decimal places. Here's an example:

number = 3.14159
formatted_number = "{:.2f}".format(number)
print(formatted_number)

 

Output:

3.14

 

We define a variable number in the code above that holds a floating-point integer with several decimal places. We then use the format() method with the string format specifier "{:.2f}" to format the number to 2 decimal places. Finally, we print the formatted number to the console.

 

Any floating-point number that you want to format can be replaced for the number variable, and the code will format it to 2 decimal places.

 

Comments 0

Leave a comment