13. Decimal places

  • Sometimes, you need to print out numbers with a set number of decimal places
  • This method is useful when working with currency values or when you are dividing numbers that may create answers with many decimal places
#get amount of pounds & pence to convert to float
pounds = float(input("Enter amount to convert: "))

#convert to euros
euros = pounds * 1.2406

#print to four decimal places
print("%.4f" % euros)

#print normally
print(euros)

Tasks

  1. Enter the code above and test it works
  2. Modify the code to print to 2 decimal places
  3. Modify the code to print € symbol before the converted amount
  4. Save, print and label your code

Extension Task

There is a function that can round numbers called round(). An example is shown below. Incorporate it into your program

x = 3.566532
print(round(x,4))