How to suppress scientific notation when printing float values?

后端 未结 12 1264
猫巷女王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条回答
  • 2020-11-22 07:35
    '%f' % (x/y)
    

    but you need to manage precision yourself. e.g.,

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

    will display zeros only.
    details are in the docs

    Or for Python 3 the equivalent old formatting or the newer style formatting

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

    Most of the answers above require you to specify precision. But what if you want to display floats like this, with no unnecessary zeros:

    1
    0.1
    0.01
    0.001
    0.0001
    0.00001
    0.000001
    0.000000000001
    

    numpy has an answer: np.format_float_positional

    import numpy as np
    
    def format_float(num):
        return np.format_float_positional(num, trim='-')
    
    0 讨论(0)
  • 2020-11-22 07:38

    Another option, if you are using pandas and would like to suppress scientific notation for all floats, is to adjust the pandas options.

    import pandas as pd
    pd.options.display.float_format = '{:.2f}'.format
    
    0 讨论(0)
  • 2020-11-22 07:39

    Since this is the top result on Google, I will post here after failing to find a solution for my problem. If you are looking to format the display value of a float object and have it remain a float - not a string, you can use this solution:

    Create a new class that modifies the way that float values are displayed.

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

    You can modify the precision yourself by changing the integer values in {:f}

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

    This will work for any exponent:

    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
    
    0 讨论(0)
  • 2020-11-22 07:45

    As of 3.6 (probably works with slightly older 3.x as well), 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 the precision calculation is to ensure we have enough precision to keep out of scientific notation (default precision is still 6).

    The dec_precision argument adds additional precision to use for decimal points. Since this makes use of the n format, no insignificant zeros will be added (unlike f formats). n also will take care of rendering already-round integers without a decimal.

    n does require float input, thus the cast.

    0 讨论(0)
提交回复
热议问题