Apr-12-2024, 03:51 PM
Since you know you'll be passing in a number and you know the format, you can make assumptions that could not be made by fpformat.extract()
import re values = [-10999999.0, -10234.0, -123.456, -10.4, -0.00137, 0.0, 0.00137, 10.4, 123.456, 10234.0, 10999999] for v in values: s, w, f, e = re.match(r'(-?)(.)\.(.+)e(.*)', f"{v:.8e}").groups() print(f"{v:11} : {s:1} {w:1} {f:8} {e}")
Output:-10999999.0 : - 1 09999990 +07
-10234.0 : - 1 02340000 +04
-123.456 : - 1 23456000 +02
-10.4 : - 1 04000000 +01
-0.00137 : - 1 37000000 -03
0.0 : 0 00000000 +00
0.00137 : 1 37000000 -03
10.4 : 1 04000000 +01
123.456 : 1 23456000 +02
10234.0 : 1 02340000 +04
10999999 : 1 09999990 +07