2017-07-27 42 views
-2

您好,我想在打印之前将变量的格式设置为如下所示,我似乎无法找到帮助正确的tut。如何格式化一个长变量以在Python中使用逗号

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) 
+1

请参阅> https://stackoverflow.com/questions/1823058/how-to-print-number-with-commas-as-thousands-separators –

+0

预期输出和您的输入(6371)的输出是什么? – balki

+0

我使用print(“The volume of the earth =”,'{:20,.2f}'。format(volume),“Kms3 \ n \ n”)修复了这个问题 –

回答

1

这固定了格式。

print ("The volume of the earth = ", '{:20,.2f}'.format(volume), "Kms3\n\n") 

Enjoy !!!

相关问题