Converting exponential to float

前端 未结 6 1436
余生分开走
余生分开走 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条回答
  •  Happy的楠姐
    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)
    

提交回复
热议问题