Skip to content Skip to sidebar Skip to footer

Converting Exponential To Float

This is my code, trying to convert the second field of the line from exponential into float. outputrrd = processrrd.communicate() (output, error) = outputrrd output_lines = output.

Solution 1:

The easy way is replace! One simple example:

value=str('6,0865000000e-01')
value2=value.replace(',', '.')
float(value2)
0.60865000000000002

Solution 2:

The reason is the use of comma in 6,0865000000e-01. This won't work because float() is not locale-aware. See PEP 331 for details.

Try locale.atof(), or replace the comma with a dot.

Solution 3:

The float is correct, just use format to display it as you want, i.e.:

print(format(the_float, '.8f'))

Solution 4:

I think it is useful to you:

defremove_exponent(value):
    """
       >>>(Decimal('5E+3'))
       Decimal('5000.00000000')
    """
    decimal_places = 8
    max_digits = 16ifisinstance(value, decimal.Decimal):
        context = decimal.getcontext().copy()
        context.prec = max_digits
        return"{0:f}".format(value.quantize(decimal.Decimal(".1") ** decimal_places, context=context))
    else:
        return"%.*f" % (decimal_places, value)

Solution 5:

Simply by casting string into float:

new_val = float('9.81E7')

Post a Comment for "Converting Exponential To Float"