3

I'm attempting to format numbers in scientific notation with exponents of base 10, e.g. write 0.00123 as 1.23x10–3, using python 3.

I found this great function which prints 1.23x10^-3, but how can the caret-exponent be replaced with a superscript?

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.

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:

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'

Marla
  • 340
  • 3
  • 16
  • where are you printing? Not everything supports superscripts (e.g., console) – Ma0 Nov 30 '18 at 07:54
  • I'd like to apply the function to a pandas dataframe. The code from https://stackoverflow.com/a/8651690/8542513 prints exponents in Jupyter Notebook. – Marla Nov 30 '18 at 07:57
  • it justs print `n` in a different line and makes it look like an (badly formatted) exponent – Ma0 Nov 30 '18 at 07:59
  • What is your desired outcome? Do you want the number as 0.001234 or 1.23x10^-3 . – Tobias Wilfert Nov 30 '18 at 08:00
  • I would like the exponent of base 10 as a superscript (see the first line of the question) – Marla Nov 30 '18 at 08:02

3 Answers3

3

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))
Richard
  • 56,349
  • 34
  • 180
  • 251
  • 1
    Your `-1` looks like `_1` though.. You also forgot a `:` on the first func def. Is there a *baby* `-`? – Ma0 Nov 30 '18 at 08:06
  • 2
    I've fixed the problems you identified, @Ev.Kounis, thank you. There is a baby `-`. Here it is `⁻`. – Richard Nov 30 '18 at 08:29
0

I take it that your main problem is how can the caret-exponent be replaced with a superscript?

If you are using python in a Jupyter notebook, there is an easy way:

  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 ...
wander95
  • 1,298
  • 1
  • 15
  • 22
0

The scinot package formats numbers in scientific notation

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⁻⁸