Printing the exponent in scientific numbers with superscript characters
The issue with the superscript numbers is that the UTF-8 codes are not contiguous, you need a translation table.
Here is one way to do it with the help of f-strings:
def scientific_superscript(num, digits=2):
base, exponent = f'{num:.{digits}e}'.split('e')
d = dict(zip('-+0123456789','⁻⁺⁰¹²³⁴⁵⁶⁷⁸⁹'))
return f'{base}⋅10{"".join(d.get(x, x) for x in exponent)}'
scientific_superscript(1.1884423896339773e-06)
# '1.19⋅10⁻⁰⁶'
scientific_superscript(3.141e123)
# '3.14⋅10⁺¹²³'
blacklisting characters:
example for 0 and +
def scientific_superscript(num, digits=2):
base, exponent = f'{num:.{digits}e}'.split('e')
d = dict(zip('-+0123456789','⁻⁺⁰¹²³⁴⁵⁶⁷⁸⁹'))
return f'{base}⋅10{"".join(d.get(x, x) for x in exponent.lstrip("0+"))}'
scientific_superscript(3.141e3)
# '3.14⋅10³'
scientific_superscript(1e103)
# '1.00⋅10¹⁰³'