将百分比显示为整数

2024-06-16 12:44:59 发布

您现在位置:Python中文网/ 问答频道 /正文

对于Python来说,这是一个新的功能,但是它为什么不能产生一个整数>;<;我在值前面添加了int,希望通过将它们具体设置为int,我不会得到40.0%的回报,我只想说40%

score = 4
opLimit = 10
total = int(100) * int(score)/int(oqLimit)
print(total,"%")

Tags: ltgt功能整数inttotalscoreprint
1条回答
网友
1楼 · 发布于 2024-06-16 12:44:59

如果你在Python3上,你应该使用字符串格式。在

score = 4
opLimit = 10
total = 100 * score/oqLimit
print("{:.0f}%".format(total))
# {...} defines a substitution field
#   :...  defines the formatting options
#    .0f    indicates to represent floats with 0 digits after the decimal

相关问题 更多 >