Reputation: 7451
I am writing a python code, which needs to asses number to very many decimal places - around 14 or 15.
I expect results that increase smoothly, for example:
0.123456789101997
0.123456789101998
0.123456789101999
0.123456789102000
0.123456789102001
But I seem to be only getting numbers (when I print them out) to 12 decimal places, so my numbers are actually jumping in steps:
0.123456789101
0.123456789101
0.123456789101
0.123456789102
0.123456789102
At first, I assumed that the number was listed to more decimal places behind the scenes, but just printed out to 12, but when I print the results, they seem to plot in steps also, rather than smoothly.
I'd be really grateful if anyone would point me in the right direction to fix this - thank you.
Upvotes: 16
Views: 88404
Reputation: 21
To display more decimal points add this to the end of your code:
print('%.60f' % value_x)
".60" indicates 60 decimal places to be displayed and "value_x" represents whatever value you want displayed.
I use this when I need to output the P-value as a real decimal number in addition to the default output which is in scientific notation.
Example:
In [1]: pearson_coef, p_value = stats.pearsonr(df['horsepower'], df['price'])
In [2]: print("The Pearson Correlation Coefficient is", pearson_coef, " with a P-value of P = ", p_value, "or ")
In [3]: print('%.50f' % p_value)
Out [4]: The Pearson Correlation Coefficient is 0.8095745670036559 with a P-value of P = 6.369057428260101e-48 or 0.00000000000000000000000000000000000000000000000637
Upvotes: 2
Reputation:
You may choose to use the module decimal for this purpose.
You need to import the module.
>>> from decimal import *
Then, you need to specify the number of decimal points you need by calling getcontext() function. The following code asks for 25 decimal points.
>>> getcontext().prec = 25
Then, you need to specify your arithmetic. The following code determines the approximate value of pi up to 25 decimal points.
>>> Decimal(22) / Decimal(7)
The output is
>>> Decimal('3.142857142857142857142857')
Upvotes: 13
Reputation: 1
x=56.0
print("{:.2f}".format(x))
If we execute this code we will get-
Output: 56.00
Upvotes: -3
Reputation: 250961
Use repr()
, print
uses str()
which reduces the number of decimal digits to 12 to make the output user friendly.
In [17]: a=0.123456789101997
In [18]: str(a)
Out[18]: '0.123456789102'
In [19]: repr(a)
Out[19]: '0.123456789101997'
or string formatting:
In [21]: "{0:.15f}".format(a)
Out[21]: '0.123456789101997'
Upvotes: 10
Reputation: 8711
As suggested in previous answers, you can use Decimal numbers via the decimal module, or alternately can specify 15 decimal places when printing floating point values, to override the default of 12 places.
In many Python implementations, ordinary floating point numbers are IEEE 754-compatible (1, 2) “binary64” double precision numbers, so have effectively 53 bits in their mantissas. As 53*math.log(2)/math.log(10)
is about 15.95, binary64 numbers support more than 15 decimal digits of precision but not quite 16.
Here is an example you can try, shown with its output:
u=1e-15
v=0.123456789101997
for k in range(13):print '{:20.15f}'.format(v+k*u)
0.123456789101997
0.123456789101998
0.123456789101999
0.123456789102000
0.123456789102001
0.123456789102002
0.123456789102003
0.123456789102004
0.123456789102005
0.123456789102006
0.123456789102007
0.123456789102008
0.123456789102009
Upvotes: 3
Reputation: 93794
How about trying the Decimal module?
In [2]: import decimal
In [3]: d = decimal.Decimal('0.123456789101997')
In [4]: print d
0.123456789101997
Upvotes: 16