Converting exponential to float

前端 未结 6 1419
余生分开走
余生分开走 2020-12-18 08:55

This is my code, trying to convert the second field of the line from exponential into float.

outputrrd = processrrd.communicate()
(output, error) = outputrrd         


        
相关标签:
6条回答
  • 2020-12-18 09:06

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

    print(format(the_float, '.8f'))
    
    0 讨论(0)
  • 2020-12-18 09:10

    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.

    0 讨论(0)
  • 2020-12-18 09:12

    I think it is useful to you:

    def remove_exponent(value):
        """
           >>>(Decimal('5E+3'))
           Decimal('5000.00000000')
        """
        decimal_places = 8
        max_digits = 16
    
        if isinstance(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)
    
    0 讨论(0)
  • 2020-12-18 09:21

    This work for me, try it out.

    def remove_exponent(value):
        decial = value.split('e')
        ret_val = format(((float(decial[0]))*(10**int(decial[1]))), '.8f')
        return ret_val
    
    0 讨论(0)
  • 2020-12-18 09:24

    Simply by casting string into float:

    new_val = float('9.81E7')
    
    0 讨论(0)
  • 2020-12-18 09:27

    The easy way is replace! One simple example:

    value=str('6,0865000000e-01')
    value2=value.replace(',', '.')
    float(value2)
    0.60865000000000002
    
    0 讨论(0)
提交回复
热议问题