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
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
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 "%"
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.