How to control the exact format of scientific notation in a Python format string?

Viewed 540

By my question I mean, I have a few numbers ranging from 0.6e9 to 3.6e9. I am already using f"foo {number:.1E} bar" to achieve scientific formatting in printing/display of f-strings, however this has the issue that 0.6e9 is "shortened" to 6.0e8, which is not what I want.

I realise that, technically, it is a correct answer but for display purposes and readability I believe it would be better served as 0.6e9, to be consistent with the others.

To be clear what I want is to control the power of 10 used in scientific formatting.
Is this something one can easily control or would it require a custom function?

1 Answers

Since you mention this is just an f-string, in your particular example where the range is limited this may suffice to have a conforming format string:

f'{number/1e9:.1f}e9'
Related