2

I want to print a big number of decimal object in python3.6

import decimal

a = decimal.Decimal('0.0')

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

The execution result I want is below.

1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111.11111111111111111111
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222.22222222222222222222
3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333.33333333333333333333
4444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444.44444444444444444444
5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555.55555555555555555555
6666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666.66666666666666666666
7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777.77777777777777777777
8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888.88888888888888888888
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999.99999999999999999999
11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111.11111111111111111110

But the actual execution result is below.

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

How can I get the results I want?

YUNO
  • 47
  • 4

1 Answers1

2

You can use string formatting, but before that you have to set precision (the default for Decimals is 28 places)

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))

Prints:

1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111.11111111111111111111
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222.22222222222222222222
3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333.33333333333333333333
4444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444.44444444444444444444
5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555.55555555555555555555
6666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666.66666666666666666666
7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777.77777777777777777777
8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888.88888888888888888888
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999.99999999999999999999
11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111.11111111111111111110
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • could you tell what is the maximum value of precision? – YUNO Dec 16 '19 at 17:42
  • @YUNO You can check https://stackoverflow.com/questions/53660036/how-to-get-largest-possible-precision-python-decimal The answer computes the largest precision programmatically (but it's large number) – Andrej Kesely Dec 16 '19 at 17:49