方案一:

使用Python处理精度很重要的浮点数时,建议使用内置的Decimal库:

from decimal import Decimal
a = Decimal('1.0231212121')
a = round(a,3) # Decimal('1.023')

方案二:

用字符串的format方法:

'{:.2f}'.format(1.0231212121) # '1.02'

 

相关文章:

  • 2021-08-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-10
  • 2022-12-23
  • 2021-12-18
  • 2021-08-15
猜你喜欢
  • 2021-06-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-24
  • 2021-06-17
相关资源
相似解决方案