-2

How would I round the answer so one decimal place would be displayed in the output? For example: If the radius were to be 8 the output would be 1005.3. As of right now it just displays 1005

import math 
import sqrt
import math

def find_volume(radius, height):
    return pi * radius * radius * height

def print_results (radius,height):
    print 'Volume = '

def find_surface_area(radius, height):
     return 2 * math.pi * radius * height + 2 * math.pi * 
     math.pow(radius,2)

def print_results (radius,height):
    print 'Surface = '

def main():
    radius = float(input("Enter value for a Radius: "))
    height = float(input("Enter value for a Height: "))
    print("Cylinder volume: %d" % (find_volume(radius, height)))
    print("Cylinder surface area: %d" % (find_surface_area(radius,height))

if __name__ == "__main__":
    main()
Tonechas
  • 13,398
  • 16
  • 46
  • 80
Shriya
  • 3
  • 3

4 Answers4

1

This question may be of help: Python 2.7: %d, %s, and float()

When you are printing your result, you are using %d which stands for an integer. Instead try %10.1f.

artbn
  • 77
  • 5
1

First there are some Syntax Errors:

print 'Surface = ' should be print('Surface= ') if you are using python 3 else this is right but then the print statements in your main functions are wrong

Also you are missing a parentheses in your last print statement in the main function and in the find_volume function you are using pi which gives also a error you have to use math.pi if you do not do from math import pi. In addition you don't have to import math twice

If you want a float with 1 decimal place you have to use %f % round(find_surface_area(radius,height),1)) but this gives, when radius is 8 and height is 3, 552.900000 but you can use .format{} instead wich works correctly for me (i'm on python 3)

the code below should work

import math

def find_volume(radius, height):

    return round(math.pi * radius * radius * height,1)

def print_results (radius,height):

   print('Volume = ')

def find_surface_area(radius, height):

   return round(2 * math.pi * radius * height + 2 * math.pi * math.pow(radius,2), 1)

def print_results (radius,height):

     print('Surface = ')

def main():

    radius = float(input("Enter value for a Radius: "))

    height = float(input("Enter value for a Height: "))

    print("Cylinder volume: {}".format(find_volume(radius, height)))

    print("Cylinder surface area: {}".format(find_surface_area(radius, height)))

if __name__ == "__main__":
    main()
optimalic
  • 511
  • 3
  • 17
0

you can do something like :

>>> a=165.1984965468
>>> "%.2f" % round(a,2)
'165.20'

EDIT

>>> "%.1f" % round(a,1)
'165.2'
Dadep
  • 2,796
  • 5
  • 27
  • 40
0

You can try this way :

res = 1005.3    
print('%.3f' %res)
# output : 1005.300
Md. Rezwanul Haque
  • 2,882
  • 7
  • 28
  • 45