Format number using LaTeX notation in Python

后端 未结 3 895
Happy的楠姐
Happy的楠姐 2021-02-05 23:19

Using format strings in Python I can easily print a number in \"scientific notation\", e.g.

>> print \'%g\'%1e9
1e+09

What is the simples

3条回答
  •  南笙
    南笙 (楼主)
    2021-02-06 00:07

    You can write a frexp10 function:

    def frexp10(x):
        exp = int(math.floor(math.log10(abs(x))))
        return x / 10**exp, exp
    

    Formatting in LaTeX style is then:

    '{0}^{{{1:+03}}}'.format(*frexp10(-1.234e9))
    

提交回复
热议问题