How to suppress scientific notation when printing float values?

后端 未结 12 1388
猫巷女王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条回答
  •  萌比男神i
    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}

提交回复
热议问题