decimal模块的作用

  Decimal模块的实例可以准确地表示任何数,对其上或其下取整,还可以限制有效数字个数。

1、Decimal创建的使用

import decimal

fmt = '{0:<25} {1:<25}'
print(fmt.format('Input', 'Output'))
print(fmt.format('-' * 25, '-' * 25))

# 输入整数
print(fmt.format(5, decimal.Decimal(5)))

# 输入字符串
print(fmt.format('3.14', decimal.Decimal('3.14')))

f = 0.1
print(fmt.format(repr(f), decimal.Decimal(str(f))))

print('{0:<23g} {1:<25}'.format(
    f,
    # 把浮点数转换成十进制数
    str(decimal.Decimal.from_float(f))[:25]))
decimal_create.py

相关文章:

  • 1970-01-01
  • 2023-03-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-08
  • 2022-12-23
  • 2023-03-11
  • 1970-01-01
  • 2022-12-23
  • 1970-01-01
相关资源
相似解决方案