Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set ipython's default scientific notation threshold

How can I modify the point at which python decides to print in scientific notation?

E.g. I'd like everything > 1e4 or < 1e-4 to be printed in scientific notation.

Good:

In [11]: 5e20
Out[11]: 5e+20

Bad:

In [12]: 5e10
Out[12]: 50000000000.0
like image 846
DilithiumMatrix Avatar asked May 31 '13 22:05

DilithiumMatrix


People also ask

How do you force scientific notation in Python?

Use str.format() on a number with ""{:e}" as str to format the number in scientific notation. To only include a certain number of digits after the decimal point, use "{:. Ne}" , where N is the desired number of digits.

How do you avoid large numbers in scientific notation in Python?

Summary: Use the string literal syntax f"{number:. nf}" to suppress the scientific notation of a number to its floating-point representation.

How do I print without scientific notation in Python?

Use a formatted string literal to print a float without scientific notation, e.g. print(f'{num:. 8f}') . You can use an expression in the f-string to print the float without scientific notation, with the specified number of decimal places.


1 Answers

In IPython you can use

%precision %.4g

this will print floating point values who's absolute value is < 1e-4 or >= 1e4 in scientific notation.

You can find more information about the %precision command in the IPython API Docs. For string formatting options have a look at the Python Docs

like image 148
zabbarob Avatar answered Sep 19 '22 11:09

zabbarob