w3resource

Python: Print the following floating numbers upto 2 decimal places


Print numbers with 2 decimal places.

Write a Python program to print the following numbers up to 2 decimal places.

Python String Exercises: Print the following floating numbers upto 2 decimal places

Sample Solution:

Python Code:

# Define a variable 'x' and assign it the value 3.1415926 (a floating-point number).
x = 3.1415926

# Define a variable 'y' and assign it the value 12.9999 (a floating-point number).
y = 12.9999

# Print an empty line for spacing.
print()

# Print the original value of 'x' with a label.
print("Original Number: ", x)

# Format the value of 'x' to two decimal places and print it with a label.
print("Formatted Number: "+"{:.2f}".format(x))

# Print the original value of 'y' with a label.
print("Original Number: ", y)

# Format the value of 'y' to two decimal places and print it with a label.
print("Formatted Number: "+"{:.2f}".format(y))

# Print an empty line for spacing.
print() 

Sample Output:

Original Number:  3.1415926                                                                                   
Formatted Number: 3.14                                                                                        
Original Number:  12.9999                                                                                     
Formatted Number: 13.00  

Flowchart:

Flowchart: Print the following floating numbers upto 2 decimal places

For more Practice: Solve these Related Problems:

  • Write a Python program to format a list of floats to two decimal places using f-strings.
  • Write a Python program to use the format() method to print numbers with exactly two decimals.
  • Write a Python program to iterate over a list of floating-point numbers and print each rounded to two decimal places.
  • Write a Python program to use the round() function within a loop to display each number with 2 decimal precision.

Go to:


Previous: Write a Python program to set the indentation of the first line.
Next: Write a Python program to print the following floating numbers upto 2 decimal places with a sign.

Python Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.