Convert float number to string with engineering notation (with SI prefix) in Python

后端 未结 5 1507
离开以前
离开以前 2021-01-12 20:16

I have a float number such as x=23392342.1

I would like to convert it to a string with engineering notation (with metric prefix)

http://en.wikip

5条回答
  •  南笙
    南笙 (楼主)
    2021-01-12 20:39

    You can use Prefixed which has a float type with additional formatting options.

    >>> from prefixed import Float
    
    >>> x = Float(23392342.1)
    >>> print(f'{x:!h}')
    23.392342 M
    
    

    Or if you want to keep 7 decimal places

    >>> print(f'{x:!.7h}')
    23.3923421 M
    
    

提交回复
热议问题