1

I was using this question to help me create a Scientific Notation function, however instead of 4.08E+10 I wanted this: 4.08 x 10^10. So I made a working function like so:

def SciNotation(num,sig):
    x='%.2e'  %num  #<-- Instead of 2, input sig here
    x= x.split('e')
    if (x[1])[0] == "-":
        return x[0]+" x 10^"+ x[1].lstrip('0')
    else:
        return x[0]+" x 10^"+ (x[1])[1:].lstrip('0')

num = float(raw_input("Enter number: "))
sig = raw_input("Enter significant figures: ")
print SciNotation(num,2)

This function, when given an input of 99999 will print an output of 1.00 x 10^5 (2 significant figures). However, I need to make use of my sig variable (# of significant figures inputted by user). I know I have to input the sig variable into Line 2 of my code but I can't seem to get to work.

So far I have tried (with inputs num=99999, sig=2):

  1. x='%.%de' %(num,sig)

    TypeError: not all arguments converted during string formatting

  2. x='%d.%de' %(num,sig)

    x = 99999.2e (incorrect output)

  3. x='{0}.{1}e'.format(num,sig)

    x = 99999.0.2e (incorrect output)

Any help would be appreciated!

Community
  • 1
  • 1
  • Are you sure you want to do this? You're reinventing the wheel here, and most languages/applications support ```4.08E+10``` notation – wnnmaw Mar 25 '15 at 16:16
  • Its just for printing the output in a more readable way. –  Mar 25 '15 at 16:18
  • 1
    How is it more readable? It's also more difficult to parse, so if you come back to it later, you're going to have to create more code to read it in too... – will Mar 25 '15 at 16:19
  • it would have been even better if the Python supported superscripts but apparently it doesn't –  Mar 25 '15 at 16:20
  • @logic There is nothing either enabling or preventing Python from outputting superscript characters. Are you printing them to an output that supports that? – Carsten Mar 25 '15 at 16:24
  • @Carsten No, I know, I was looking into the [sympy method](http://stackoverflow.com/questions/8651361/how-do-you-print-superscript-in-python), but I didn't feel like installing a new module. its ok for now and thank you for the answer :) –  Mar 25 '15 at 16:27

2 Answers2

8

If you must do this, then the easiest way will be to just use the built in formating, and then just replace the e+05 or e-12 with whatever you'd rather have:

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

print sci_notation(10000, sig_fig=4)
# 1.0000 * 10^4
jwalton
  • 5,286
  • 1
  • 18
  • 36
will
  • 10,260
  • 6
  • 46
  • 69
  • I'm not sure why OP accepted the other answer to the question. this was clearly the intended result. – jwalton Jul 13 '20 at 15:04
1

Use the new string formatting. The old style you're using is deprecated anyway:

In [1]: "{0:.{1}e}".format(3.0, 5)
Out[1]: '3.00000e+00'
Carsten
  • 17,991
  • 4
  • 48
  • 53
  • 4
    "instead of 4.08E+10 I wanted this: 4.08 x 10^10" - this is exactly what you didn't want... – will Mar 25 '15 at 16:30
  • @logic quite right. At any rate, i think your routine can be cleaned up, take a look at what i have below. It could be done in one line, but would be a little messier. – will Mar 25 '15 at 16:35