84669인 학습
152542인 학습
20005인 학습
5487인 학습
7821인 학습
359900인 학습
3350인 학습
180660인 학습
48569인 학습
18603인 학습
40936인 학습
1549인 학습
1183인 학습
32909인 학습
我将[-0.32695389161796801, -0.31471406408825409, -0.31475407980700348]中的每个元素进行先保留小数点后3位再保留小数点后2位的操作,用如round(round(-0.32695389161796801,3),2)的方式,得到[-0.33000000000000002, -0.32000000000000001, -0.32000000000000001],但我想得到的是[-0.33, -0.32, -0.32],谢谢
认证高级PHP讲师
在使用Python处理精度很重要的浮点数时,建议使用内置的Decimal库:
from decimal import Decimal a = Decimal('1.0231212121') a = round(a,3) # Decimal('1.023')
如果只是要求看起来“精确”,那么也可以用字符串的format方法
'{:.2f}'.format(1.0231212121) # '1.02'
在使用Python处理精度很重要的浮点数时,建议使用内置的Decimal库:
如果只是要求看起来“精确”,那么也可以用字符串的format方法