【问题标题】:How to format a long variable to use commas in python如何格式化长变量以在python中使用逗号
【发布时间】: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


【解决方案1】:

这修正了格式。

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

享受!!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-23
    • 1970-01-01
    • 2010-10-16
    • 2012-07-26
    • 2018-07-18
    • 1970-01-01
    • 2020-07-11
    相关资源
    最近更新 更多