2017-07-27 3 views
-2

こんにちは、以下のように可変ボリュームをフォーマットする前に、正しく機能することができません。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}"フォーマット(ボリューム)、 "Kms3 \ n \ n") –

答えて

1

これにより、書式設定が修正されました。

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

お楽しみください!

関連する問題