简体   繁体   English

Python:带上标指数的科学计数法

[英]Python: scientific notation with superscript exponent

I'm attempting to format numbers in scientific notation with exponents of base 10, eg write 0.00123 as 1.23x10 –3 , using python 3.我正在尝试使用以 10 为底的指数来格式化科学记数法中的数字,例如将 0.00123 写为 1.23x10 –3 ,使用 python 3。

I found this great function which prints 1.23x10^-3, but how can the caret-exponent be replaced with a superscript?我发现这个很棒的 function 打印出 1.23x10^-3,但是如何用上标替换插入符指数呢?

def sci_notation(number, sig_fig=2):
    ret_string = "{0:.{1:d}e}".format(number, sig_fig)
    a,b = ret_string.split("e")
    b = int(b)         # removed leading "+" and strips leading zeros too.
    return a + "x10^" + str(b)

print(sci_notation(0.001234, sig_fig=2))      # Outputs 1.23x10^-3

The function is modified from https://stackoverflow.com/a/29261252/8542513 . function 是从https://stackoverflow.com/a/29261252/8542513修改而来的。

I've attempted to incorporate the answer from https://stackoverflow.com/a/8651690/8542513 to format the superscript, but I'm not sure how sympy works with variables:我试图合并来自https://stackoverflow.com/a/8651690/8542513的答案来格式化上标,但我不确定 sympy 如何处理变量:

from sympy import pretty_print as pp, latex
from sympy.abc import a, b, n

def sci_notation(number, sig_fig=2):
  ret_string = "{0:.{1:d}e}".format(number, sig_fig)
  a,b = ret_string.split("e")
  b = int(b)             #removed leading "+" and strips leading zeros too.
  b = str(b)
  expr = a + "x10"**b    #Here's my problem
  pp(expr)               # default
  pp(expr, use_unicode=True)
  return latex(expr)

print(latex(sci_notation(0.001234, sig_fig=2))) 

This returns: TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'这将返回:TypeError:** 或 pow() 不支持的操作数类型:'str' 和 'int'

Here's a simple solution: 这是一个简单的解决方案:

def SuperScriptinate(number):
  return number.replace('0','⁰').replace('1','¹').replace('2','²').replace('3','³').replace('4','⁴').replace('5','⁵').replace('6','⁶').replace('7','⁷').replace('8','⁸').replace('9','⁹').replace('-','⁻')

def sci_notation(number, sig_fig=2):
    ret_string = "{0:.{1:d}e}".format(number, sig_fig)
    a,b = ret_string.split("e")
    b = int(b)         # removed leading "+" and strips leading zeros too.
    return a + "x10^" + SuperScriptinate(str(b))

I take it that your main problem is how can the caret-exponent be replaced with a superscript? 我认为你的主要问题是how can the caret-exponent be replaced with a superscript?

If you are using python in a Jupyter notebook, there is an easy way: 如果你在Jupyter笔记本中使用python,有一个简单的方法:

  from IPython.display import display, Math, Latex

  # if the number is in scientific format already
  display(Math('2.14e-6'.replace('e', r'\times 10^{') + '}'))

  # if it is not:
  d = "%e" % (number)
  # then use the above form: display(Math(d.replace('e', r'\times ...

The scinot package formats numbers in scientific notation scinot package 以科学记数法格式化数字

import scinot as sn
a=4.7e-8
print(scinot.format(a))

4.7 × 10⁻⁸

or in a string或者在一个字符串中

print('The value is {0:s}'.format(scinot.format(a)))

The value is 4.7 × 10⁻⁸

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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