fork download
  1. from decimal import Decimal, ROUND_HALF_UP
  2.  
  3. # 方法3:Decimal精确控制
  4. num = Decimal('3.1415926')
  5. result = num.quantize(Decimal('0.00'), rounding=ROUND_HALF_UP) # 输出Decimal('3.14')
  6. print(result)
  7.  
  8. # 方法4:numpy的around
  9. import numpy as np
  10. arr = np.array([3.141, 2.718])
  11. result = np.around(arr, 2) # 输出array([3.14, 2.72])
  12. print(result)
Success #stdin #stdout 0.8s 41872KB
stdin
Standard input is empty
stdout
3.14
[3.14 2.72]