How to suppress scientific notation when printing float values?

后端 未结 12 1265
猫巷女王i
猫巷女王i 2020-11-22 07:12

Here\'s my code:

x = 1.0
y = 100000.0    
print x/y

My quotient displays as 1.00000e-05.

Is there any way to suppress

相关标签:
12条回答
  • This is using Captain Cucumber's answer, but with 2 additions.

    1) allowing the function to get non scientific notation numbers and just return them as is (so you can throw a lot of input that some of the numbers are 0.00003123 vs 3.123e-05 and still have function work.

    2) added support for negative numbers. (in original function, a negative number would end up like 0.0000-108904 from -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
    
    0 讨论(0)
  • 2020-11-22 07:47

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

    '{0:f}'.format(x/y)
    
    0 讨论(0)
  • 2020-11-22 07:50

    If it is a string then use the built in float on it to do the conversion for instance: print( "%.5f" % float("1.43572e-03")) answer:0.00143572

    0 讨论(0)
  • 2020-11-22 07:51

    In addition to SG's answer, you can also use the Decimal module:

    from decimal import Decimal
    x = str(Decimal(1) / Decimal(10000))
    
    # x is a string '0.0001'
    
    0 讨论(0)
  • 2020-11-22 07:54

    Using the newer version ''.format (also remember to specify how many digit after the . you wish to display, this depends on how small is the floating number). See this example:

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

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

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

    Update

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

    >>> f'{a:.20f}'
    '-0.00000000000000007186'
    
    0 讨论(0)
  • 2020-11-22 07:56

    Using 3.6.4, I was having a similar problem that randomly, a 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 that I had to do to fix it was to add 'f':

    fout.write('someFloats: {0:0.8f},{1:0.8f},{2:0.8f}'.format(someFloat[0], someFloat[1], someFloat[2]))
    
    0 讨论(0)
提交回复
热议问题