Apr-12-2024, 01:41 PM
I have some python 2.7 code that I need to update to 3.10. The code used fpformat.extract() to determine the following:
Python 2.7 Code
Is there another class, or methods of either floats or strings that is specifically designed to replace fpformat.extract()?
I have search quite extensively for solutions to this question.
Any guidance will be greatly appreciated.
Python 2.7 Code
floatLst=[-10999999.0,-10234.0,-123.456,-10.4,-0.00137,0.0,0.00137,10.4,123.456,10234.0,10999999] fmt = "%.8E" for numFloat in floatLst: sign, intPart, fractPart, exponent = fpformat.extract(fmt % numFloat) print (sign,intPart,fractPart,exponent)The closet equivalent that I've found so far in Python 3.10 uses Decimal.
from decimal import * floatLst=[-10999999.0,-10234.0,-123.456,-10.4,-0.00137,0.0,0.00137,10.4,123.456,10234.0,10999999] for numFloat in floatLst: sign,digits,exponent = Decimal(numFloat).as_tuple() print (numFloat,digits,exponent,len(digits))This definitely does not work as well. Due to rounding both digits and exponent can be confusing.
Is there another class, or methods of either floats or strings that is specifically designed to replace fpformat.extract()?
I have search quite extensively for solutions to this question.
Any guidance will be greatly appreciated.