Skip to content

Instantly share code, notes, and snippets.

@svenk
Created September 10, 2018 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svenk/5bddb4e3484e93ebc71b97574779de60 to your computer and use it in GitHub Desktop.
Save svenk/5bddb4e3484e93ebc71b97574779de60 to your computer and use it in GitHub Desktop.
Python: Display scientific number with 10 power something (suitable for LaTeX) instead of the "e" suffix
import re
sci = lambda d: re.sub(r'e\+?(-?)?0?(\d+)', r" x 10^{\1\2}" ,"%.2e"%d)
"""
### Examples:
In [34]: sci(1)
Out[34]: '1.00 x 10^{0}'
In [35]: sci(123456)
Out[35]: '1.23 x 10^{5}'
In [36]: sci(0.0000123456)
Out[36]: '1.23 x 10^{-5}'
In [37]: sci(0.0033456)
Out[37]: '3.35 x 10^{-3}'
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment