-2

Hi I'm wondering how to get python to output an answer to many more decimal places than the default value.

For example: Currently print(1/7) outputs: 0.14285714285

I want to be able to output:0.142857142857142857142857142857142857142857142857

  • https://docs.python.org/3.4/library/decimal.html – TigerhawkT3 Jun 26 '15 at 20:58
  • possible duplicate of [How to define a decimal class holding 1000 digits in python?](http://stackoverflow.com/questions/19980840/how-to-define-a-decimal-class-holding-1000-digits-in-python) – TigerhawkT3 Jun 26 '15 at 20:59

2 Answers2

3

If you want completely arbitrary precision (which you will pretty much need to get at that level of precision), I recommend looking at the mpmath module.

>>> from mpmath import mp
>>> mp.dps = 100
>>> mp.fdiv(1.0,7.0)
mpf('0.1428571428571428571428571428571428571428571428571428571428571428571428571428571428571428571428571428579')

I suppose if all you want is to be able to do very simple arithmetic, the builtin decimal module will suffice. I would still recommend mpmath for anything more complex. You could try something like this:

>>> import decimal
>>> decimal.setcontext(decimal.Context(prec=100))
>>> decimal.Decimal(1.0) / decimal.Decimal(7.0)
Decimal('0.1428571428571428571428571428571428571428571428571428571428571428571428571428571428571428571428571429')
Blair
  • 6,623
  • 1
  • 36
  • 42
1

You need to use the decimal module: https://docs.python.org/3.5/library/decimal.html

>>> from decimal import Decimal
>>> Decimal(1) / Decimal(7)
Decimal('0.1428571428571428571428571429')
David Wolever
  • 148,955
  • 89
  • 346
  • 502