Display decimal in scientific notation

How can I display this:

Decimal ('40800000000.00000000000000') as '4.08E + 10'?

I tried this:

>>> '%E' % Decimal('40800000000.00000000000000') '4.080000E+10' 

But he has an extra 0.

+123
python string-formatting
Aug 02 2018-11-11T00:
source share
10 answers
 from decimal import Decimal '%.2E' % Decimal('40800000000.00000000000000') # returns '4.08E+10' 

Your โ€œ40800000000.00000000000000โ€ has many more significant zeros that have the same meaning as any other digit. That is why you should clearly indicate where you want to stay.

If you want to automatically delete all trailing zeros, you can try:

 def format_e(n): a = '%E' % n return a.split('E')[0].rstrip('0').rstrip('.') + 'E' + a.split('E')[1] format_e(Decimal('40800000000.00000000000000')) # '4.08E+10' format_e(Decimal('40000000000.00000000000000')) # '4E+10' format_e(Decimal('40812300000.00000000000000')) # '4.08123E+10' 
+123
Aug 02 2018-11-11T00:
source share

Here is an example using the format() function:

 >>> "{:.2E}".format(Decimal('40800000000.00000000000000')) '4.08E+10' 
+92
Nov 08 '13 at 16:44
source share

Given your number

 x = Decimal('40800000000.00000000000000') 

Starting with Python 3,

 '{:.2e}'.format(x) 

is the recommended way to do this.

e means you need scientific notation, and .2 means you need 2 digits after the dot. So you get x.xxEยฑn

+23
Mar 12 '17 at 15:47
source share

No one mentioned the short form of the .format method:

Requires at least Python 3.6

 f"{Decimal('40800000000.00000000000000'):.2E}" 

(I believe it just like Siz Timmerman, just a little shorter)

+16
Feb 26 '18 at 10:38
source share

See tables from Python String Formatting to choose the correct format. In your case, this is %.2E .

+8
Aug 02 2018-11-11T00:
source share

My decimals are too big for %E , so I had to improvise:

 def format_decimal(x, prec=2): tup = x.as_tuple() digits = list(tup.digits[:prec + 1]) sign = '-' if tup.sign else '' dec = ''.join(str(i) for i in digits[1:]) exp = x.adjusted() return '{sign}{int}.{dec}e{exp}'.format(sign=sign, int=digits[0], dec=dec, exp=exp) 

Here's a usage example:

 >>> n = decimal.Decimal(4.3) ** 12314 >>> print format_decimal(n) 3.39e7800 >>> print '%e' % n inf 
+4
Jan 06 '13 at
source share

This worked better for me:

 import decimal '%.2E' % decimal.Decimal('40800000000.00000000000000') # 4.08E+10 
+3
May 12 '16 at 8:09 a.m.
source share

To convert decimal to scientific notation without having to specify precision in the format string and without including trailing zeros, I currently use

 def sci_str(dec): return ('{:.' + str(len(dec.normalize().as_tuple().digits) - 1) + 'E}').format(dec) print( sci_str( Decimal('123.456000') ) ) # 1.23456E+2 

To preserve any trailing zeros, simply remove normalize() .

+2
Feb 26 '14 at 11:37
source share
 def formatE_decimal(x, prec=2): """ Examples: >>> formatE_decimal('0.1613965',10) '1.6139650000E-01' >>> formatE_decimal('0.1613965',5) '1.61397E-01' >>> formatE_decimal('0.9995',2) '1.00E+00' """ xx=decimal.Decimal(x) if type(x)==type("") else x tup = xx.as_tuple() xx=xx.quantize( decimal.Decimal("1E{0}".format(len(tup[1])+tup[2]-prec-1)), decimal.ROUND_HALF_UP ) tup = xx.as_tuple() exp = xx.adjusted() sign = '-' if tup.sign else '' dec = ''.join(str(i) for i in tup[1][1:prec+1]) if prec>0: return '{sign}{int}.{dec}E{exp:+03d}'.format(sign=sign, int=tup[1][0], dec=dec, exp=exp) elif prec==0: return '{sign}{int}E{exp:+03d}'.format(sign=sign, int=tup[1][0], exp=exp) else: return None 
0
Jan 12 '15 at 10:19
source share

Or just (python 3.6+):

 f"{40800000000.00000000000000:.2E}" output: 4.08E+10 

or hard_to_read_number = 40800000000.00000000000000

 f"{difficult_to_read_number:.2E}" output: 4.08E+10 
0
May 03 '19 at 10:05
source share



All Articles