1

I have a question for python, so I have a program that prints out the volume of a shape, I was able to make the volume to two decimal places using round(volume3, 2)) and it comes out fine. However, my program continuously asks for the user for input on lengths for the shape and then calculates the volume. So, it will say :

input length:

input length: 

and then it will calulate the volume and say for example,

"your volume is: 34.22"

however, at the end when I want to print all the recorded volumes for the shape, it'll print out 54.3487847384783748 and I only want 2 decimal places!

how can I do this? this is what i have so far

print("shape: ", *shapeVolumes, end='')
print(*shape, sep=', ')

and it'll print out 54.334343434, 32.6676767, 32.4545454

but id like: 54.33, 32.66, 32.45 (example numbers)

lycuid
  • 2,555
  • 1
  • 18
  • 28
Melanie432k
  • 53
  • 1
  • 1
  • 7

2 Answers2

4

you probably need the format method (recommended):

"{:.2f}".format(54.334343434)

you also have the legacy method (avoid, even if it reminds you of C format, now format is preferred (and more powerful):

"%.2f" % 54.334343434
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
2
print round(str, number_of_decimal)

or if you want to keep it as it is and just print the decimal:

print "%.2f" % str #replace 2 with as many as you want 

If in Python 3 just wrap the prints with ()

MooingRawr
  • 4,901
  • 3
  • 24
  • 31