Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scientific Notation formatting in Python

How can get following formatting (input values are always less than 0.1):

> formatting(0.09112346)
0.91123E-01
> formatting(0.00112346)
0.11234E-02

and so on.

I am looking for some elegant solution. I am already using a custom function given below:

def formatting(x, prec=5):
    tup    = x.as_tuple()
    digits = list(tup.digits[:prec])
    dec    = ''.join(str(i) for i in digits)
    exp    = x.adjusted()
    return '0.{dec}E{exp}'.format(dec=dec, exp=exp)
like image 449
ARK4579 Avatar asked Nov 13 '18 06:11

ARK4579


People also ask

What is %s and %D in Python?

%s is used as a placeholder for string values you want to inject into a formatted string. %d is used as a placeholder for numeric or decimal values. For example (for python 3) print ('%s is %d years old' % ('Joe', 42))

What is %s %d %F in Python?

Answer. In Python, string formatters are essentially placeholders that let us pass in different values into some formatted string. The %d formatter is used to input decimal values, or whole numbers. If you provide a float value, it will convert it to a whole number, by truncating the values after the decimal point.

How do you format exponential values in Python?

Just "{:. 2e}". format(n) returns '3.39e+7800' in Python 3.3. 2 (v3.

What is scientific notation in Python?

Python Scientific notation is a way of writing a large or a small number in terms of powers of 10. To write a number in scientific notation the number is between 1 and 10 is multiplied by a power of 10 (a * 10^b). This method can be used to initialize a number in a small format.

How to suppress scientific notation when using float values in Python?

There is a simple technique to suppress scientific notation when using float values by using the %f flag in string. This will convert the number into a floating-point number and then print it. Note that this method has a precision of 10^-6 only.

Why are the numbers printed in standard notation in Python?

Notice: the number is printed in standard notation even though we defined it with scientific notation. This is because Python decides whether to display numbers in scientific notation based on what number it is.

How to format a string in scientific form in Python?

Typically, the format () function is used when you want to format strings in a specific format. In the code chunk above, the use of the format () function is pretty straightforward. The first parameter was the (small) number we wanted to represent in scientific form in Python.


1 Answers

You can use the format() function. The format specification mentions it there:

'E' - Exponent notation. Same as 'e' except it uses an upper case ‘E’ as the separator character.

>>> print('{:.5E}'.format(0.09112346))
9.11235E-02
>>> print('{:.5E}'.format(0.00112346))
1.12346E-03

However it isn't quite like the output you have in your answer. If the above is not satisfactory, then you might use a custom function to help (I'm not the best at this, so hopefully it's ok):

def to_scientific_notation(number):
    a, b = '{:.4E}'.format(number).split('E')
    return '{:.5f}E{:+03d}'.format(float(a)/10, int(b)+1)

print(to_scientific_notation(0.09112346))
# 0.91123E-01
print(to_scientific_notation(0.00112346))
# 0.11234E-02
like image 200
Jerry Avatar answered Nov 15 '22 20:11

Jerry