python - 如何在python3中精确打印大量小数对象?

标签 python python-3.x

我想在python3.6中打印大量的十进制对象

import decimal

a = decimal.Decimal('0.0')

for idx in range(10):
    a += decimal.Decimal('1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111.11111111111111111111')
    print(a)

我想要的执行结果如下。

1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111.11111111111111111111
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222.22222222222222222222
3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333.33333333333333333333
4444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444.44444444444444444444
5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555.55555555555555555555
6666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666.66666666666666666666
7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777.77777777777777777777
8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888.88888888888888888888
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999.99999999999999999999
11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111.11111111111111111110

但实际执行结果如下。

1.111111111111111111111111111E+99
2.222222222222222222222222222E+99
3.333333333333333333333333333E+99
4.444444444444444444444444444E+99
5.555555555555555555555555555E+99
6.666666666666666666666666666E+99
7.777777777777777777777777777E+99
8.888888888888888888888888888E+99
9.999999999999999999999999999E+99
1.111111111111111111111111111E+100

怎样才能得到我想要的结果?

最佳答案

您可以使用字符串格式,但在此之前您必须设置精度(小数默认为 28 位)

import decimal

a = decimal.Decimal('0.0')

with decimal.localcontext() as ctx:
    ctx.prec = 120

    for idx in range(10):
        a += decimal.Decimal('1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111.11111111111111111111')
        print('{:.20f}'.format(a))

打印:

1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111.11111111111111111111
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222.22222222222222222222
3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333.33333333333333333333
4444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444.44444444444444444444
5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555.55555555555555555555
6666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666.66666666666666666666
7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777.77777777777777777777
8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888.88888888888888888888
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999.99999999999999999999
11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111.11111111111111111110

关于python - 如何在python3中精确打印大量小数对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59361496/

相关文章:

Python 创建 zip 文件

python - @unique 装饰器在 python 中有什么作用?

python - 尝试安装 kornia 时无法找到 nvidia-cudnn-cu11 的安装候选

python - 动态命名指向类的变量

python - 比较Python中2个不同列表中的单词,并在输出中用 "*"替换找到的单词

python - 多处理导致错误的无限循环

python-3.x - 如何将秒转换成hh :mm:ss in python 3. x

python - 需要帮助使用 lxml 和 xpath 抓取 NHL 统计表

python - 二维数组 matplotlib 的颜色图

python - 使用服务帐户凭据的 GDrive 导出失败并显示 404