Home > Software design >  How do change my program so that it always rounds to the nearest 2 decimal places)
How do change my program so that it always rounds to the nearest 2 decimal places)

Time:10-18

#.This program will find the area and perimeter of a circle with a radius given by the user.

import math

radius=int(input('enter a number for radius'))

area=math.piradius2

print('area of circle %f'%area)

perimeter= 2math.piradius

print('perimeter of circle %f'% perimeter)

CodePudding user response:

Instead of %f, use %.2f when printing to round to two decimal places.

CodePudding user response:

Python has a round keyword. Use it like this:

round(3.14159, 2)
>>> 3.14

Alternatively, you can use python f-strings to round when printing, like this:

num = 3.14159
print(f'{num:.2f}')
>>> 3.14
  • Related