For class, I had to write a simple program that would calculate the surface area and volume of a torus. I did that, but my answers are long decimal numbers. How do I get the answer to round off?

Thanks in Advance

Recommended Answers

If you want to round the number for printed output, the correct way is to use string formatting operations. Click here to see some examples.

Jump to Post

Another way ...

# round floats
x = 355/113.0
print(x)
print(round(x, 2))
Jump to Post

All 3 Replies

If you want to round the number for printed output, the correct way is to use string formatting operations. Click here to see some examples.

Another way ...

# round floats
x = 355/113.0
print(x)
print(round(x, 2))

Use integer division. When you are dividing to get the surface area, use "//" instead of just "/". This will round your number. To get remainders, use "%"