简体   繁体   中英

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

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?

由于您提到这只是一个 f 字符串,因此在您的特定示例中,范围受到限制,这可能足以具有一致的格式字符串:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM