0%

python小数保留



1.使用round函数

1
2
3
number = 3.14159
rounded_number = round(number, 2)
print(rounded_number) # 输出 3.14

2.使用格式化字符串

1
2
3
number = 3.14159
formatted_number = "{:.2f}".format(number)#:表示字符串的规则
print(formatted_number) # 输出 3.14