How to suppress scientific notation in Python?

Here is my code:

x = 1.0 y = 100000.0 print x/y 

My ratio is displayed as 1.00000e-05

Is there a way to suppress a scientific record and make it display 0.00001 ? I am going to use the result as a string.

+124
python floating-point
Mar 18 '09 at 15:27
source share
11 answers
 '%f' % (x/y) 

but you need to control precision precisely. eg.

 '%f' % (1/10**8) 

only zeros will be displayed.
details are in the docs

Either for Python 3 the equivalent old formatting or

+53
Mar 18 '09 at 3:30 p.m.
source share
β€” -

Using the newer version ''.format (also do not forget to indicate how many digits after . You want to display, it depends on how small the floating number is). See this example:

 >>> a = -7.1855143557448603e-17 >>> '{:f}'.format(a) '-0.000000' 

as shown above, the default value is 6 digits! This is not useful for our example, so instead we could use something like this:

 >>> '{:.20f}'.format(a) '-0.00000000000000007186' 

Update

Starting with Python 3.6, this can be simplified with a new formatted string literal as follows:

 >>> f'{a:.20f}' '-0.00000000000000007186' 
+65
Oct 19 '15 at 16:41
source share

With newer versions of Python (2.6 and later), you can use ''.format() to accomplish what @SilentGhost suggested:

 '{0:f}'.format(x/y) 
+46
Mar 28 '11 at 19:05
source share

Another option if you use pandas and want to disable the scientific designation for all floats is to configure the pandas.

 import pandas as pd pd.options.display.float_format = '{:.2f}'.format 
+11
May 24 '19 at 3:34
source share

This will work for any metric:

 def getExpandedScientificNotation(flt): str_vals = str(flt).split('e') coef = float(str_vals[0]) exp = int(str_vals[1]) return_val = '' if int(exp) > 0: return_val += str(coef).replace('.', '') return_val += ''.join(['0' for _ in range(0, abs(exp - len(str(coef).split('.')[1])))]) elif int(exp) < 0: return_val += '0.' return_val += ''.join(['0' for _ in range(0, abs(exp) - 1)]) return_val += str(coef).replace('.', '') return return_val 
+4
Aug 10 '17 at 3:56 on
source share

This uses the Captain Cucumber answer , but with two additions.

1) letting the function get the numbers of non-scientific notations and just return them as is (so you can add a lot of input that some numbers are 0.00003123 versus 3.123e-05 and still work).

2) added support for negative numbers. (in the original function, a negative number will end as 0.0000 -1 08904 out of -1.08904e-05)

 def getExpandedScientificNotation(flt): was_neg = False if not ("e" in flt): return flt if flt.startswith('-'): flt = flt[1:] was_neg = True str_vals = str(flt).split('e') coef = float(str_vals[0]) exp = int(str_vals[1]) return_val = '' if int(exp) > 0: return_val += str(coef).replace('.', '') return_val += ''.join(['0' for _ in range(0, abs(exp - len(str(coef).split('.')[1])))]) elif int(exp) < 0: return_val += '0.' return_val += ''.join(['0' for _ in range(0, abs(exp) - 1)]) return_val += str(coef).replace('.', '') if was_neg: return_val='-'+return_val return return_val 
+4
Sep 19 '17 at 10:40
source share

Besides the SG answer, you can also use the Decimal module:

 from decimal import Decimal x = str(Decimal(1) / Decimal(10000)) # x is a string '0.0001' 
+1
Mar 18 '09 at 15:34
source share

If it is a string , then use the built-in float on it to convert, for example: print( "%.5f" % float("1.43572e-03")) Answer: 0.00143572

+1
Feb 06 '15 at 8:43
source share

Using 3.6.4, I had a similar problem that randomly the number in the output file would be formatted with scientific notation when using this:

 fout.write('someFloats: {0:0.8},{1:0.8},{2:0.8}'.format(someFloat[0], someFloat[1], someFloat[2])) 

All I had to do to fix this was add 'f':

 fout.write('someFloats: {0:0.8f},{1:0.8f},{2:0.8f}'.format(someFloat[0], someFloat[1], someFloat[2])) 
0
Apr 13 '18 at 23:07 on
source share

Since this is the best result on Google, I will post here without finding a solution to my problem. If you want to format the displayed value of a floating-point object and leave it floating-point rather than a string, you can use this solution:

Create a new class that changes the way floating point values ​​are displayed.

 from builtins import float class FormattedFloat(float): def __str__(self): return "{:.10f}".format(self).rstrip('0') 

You can change the accuracy yourself by changing the integer values ​​in {:f}

0
Jul 02 '18 at 17:16
source share

Starting from version 3.6 (it may also work with older versions 3.x), this is my solution:

 import locale locale.setlocale(locale.LC_ALL, '') def number_format(n, dec_precision=4): precision = len(str(round(n))) + dec_precision return format(float(n), f'.{precision}n') 

The purpose of calculating precision is to ensure that we have enough accuracy to not fall into scientific notation (the default accuracy is still 6).

The dec_precision argument adds extra precision for decimal points. Since the format n used for this, minor zeros will not be added (unlike the f formats). n also take care of rendering already round integers without a decimal.

n requires a float input, so a cast.

0
Jul 22 '19 at 1:52
source share



All Articles