I have a large dictionary which contains lots of the decimal data type. Many of these values are very small and when printed are displayed in scientific notation. How do I get the dictionary to print as a normal decimal without printing each piece of data individually? Thanks
from decimal import Decimal
dic = {'value1': Decimal('0.000000123'), 'value2' : Decimal('0.00000001'),
'value3' : Decimal('0.00000000000001')}
# This happens which I don't want:
print(dic)
>> {'value1': Decimal('1.23E-7'), 'value2': Decimal('1E-8'),
'value3': Decimal('1E-14')}
# I want:
print(dic)
>> {'value1': Decimal('0.000000123'), 'value2' : Decimal('0.00000001'),
'value3' : Decimal('0.00000000000001')}
I cannot use {0:.8f}.format(val) or anything like that on each item individually as there are thousands of items so how can I get values to print without scientific notation? Maybe on creation of the decimal? Thanks
Edit: there are also other datatypes in the dictionary such as strings so a solution cannot be affected by this.
If it's really just for looking at your values, then you can do something like this:
import numbers
# Include isinstance() to check that your value is numeric
print(['{}: {:.14f}'.format(k,i) for k,i in dic.items()
if isinstance(i, numbers.Number)])
Which returns:
['value1: 0.00000012300000', 'value2: 0.00000001000000', 'value3: 0.00000000000001']
Or, if you still want to print non-numeric values (just not formatted), use:
print(['{}: {:.14f}'.format(k,i) if isinstance(i, numbers.Number)
else '{}: {}'.format(k,i) for k,i in dic.items()])
Example:
dic = {'value1': Decimal('0.000000123'), 'value2' : Decimal('0.00000001'),
'value3' : Decimal('0.00000000000001'), 'value4': 'string'}
print(['{}: {:.14f}'.format(k,i) if isinstance(i, numbers.Number)
else '{}: {}'.format(k,i) for k,i in dic.items()])
returns:
['value1: 0.00000012300000', 'value2: 0.00000001000000', 'value3: 0.00000000000001', 'value4: string']
But as you noted, this is only good for viewing your values, as they are returned as strings of your keys and values combined.
Note:
I used {:.14f}
because your smallest decimal was on the order of E-14