【问题标题】:How to print a certain amount of characters of a number in python [duplicate]如何在python中打印一定数量的数字字符[重复]
【发布时间】:2017-07-04 12:50:29
【问题描述】:

我正在做一个百分比查找器,非常基础。但是,我只想在 float 或 int 中打印 4 个字符。任何人都可以帮忙吗? 到目前为止,这是我的代码:

    numer = int(input("What's the numerator?"))
    denom = int(input("What's the denominator?"))
    percent = (numer / denom * 100, "%")
    print(percent)

我希望 5/6 的输出为:

    83.33%

但它实际上是打印出来的:

    83.33333333333334%

【问题讨论】:

    标签: python-3.x


    【解决方案1】:

    您可以在打印功能中使用 %

    print("%.2f"%(a/b))
    

    【讨论】:

    • 谢谢!成功了!
    猜你喜欢
    • 2012-12-30
    • 1970-01-01
    • 1970-01-01
    • 2016-06-30
    • 1970-01-01
    • 1970-01-01
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多