Python 中的百分比四舍五入

use*_*353 5 python

if act == "block" and enemy_decision != 2:
    percentage_blocked = (enemy_attack - block)/(enemy_attack) * 100
    print("You have blocked %s percent of the enemy's attack." % percentage_blocked)
Run Code Online (Sandbox Code Playgroud)

由此我得到诸如 82.113124523242323 之类的数字。我怎样才能把这个百分比四舍五入到第10位,例如82.1。

the*_*eye 3

您可以像这样使用模板字符串{0:.1f},这意味着第一个参数必须格式化为 1 位十进制数字

print("You have blocked {0:.1f} percent of the enemy's attack.".format(percentage_blocked))
Run Code Online (Sandbox Code Playgroud)