【发布时间】:2017-07-27 23:43:07
【问题描述】:
您好,我想在打印可变音量之前对其进行格式化,如下所示,我似乎无法找到正确帮助的 tut。
def volume_sphere(radius):
"""radius = 12,742 / 2 (earth diameter)"""
pi = 3.141592653589
volume = ( 4 / 3 ) * pi * radius ** 3
format('{:20,.2f}'.format(volume))
# volume format 0,000,000,000,000,000,000
print ("Calculate volume of a sphere (The Earth) \nvolume = ( 4 / 3 ) * pi * radius ** 3")
print ("Radius = ", radius, "Kms")
print ("The volume of the earth = ", volume, "Kms3\n\n")
volume_sphere(6371)
【问题讨论】:
-
您的输入 (6371) 的预期输出和输出是什么?
-
我用 print ("The volume of the earth = ", '{:20,.2f}'.format(volume), "Kms3\n\n") 修复了它
-
现在我想在调用 print 之前格式化变量,这样我就不必在 print 中进行格式化,并且无论我从哪里调用它,变量总是正确的
-
为什么我收到了一个负三???
标签: python-3.x floating-point format comma