I need to cast the string “0.00003” to a float where the result is 0.00003, and not the scientific notation of 3e-5
float() has a “feature” that any value passed in with 4 or more leading zero’s after the decimal returns a value in scientific notation.
Any value passed to float() with less than 4 zero’s after the decimal point results in a float.
float(“0.0003”) results in 0.0003, type(float(“0.0003”)) returns <class ‘float’>
Using float() with 4 or more zero’s after the decimal point results in a scientific notation value.
float(“0.00003”) results in a float of 3e-5
Yes, 3e-5 is value equivalent to 0.00003, but my use case requires that the conversion results in a float data type with the format of 0.00003, and not in scientific notation.
I can’t use a formatter like f'{float(“0.00003”):.5f}’ since this returns a string, and I need a float.
MikeD187 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.