Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress scientific notation when printing float values?

Here's my code:

x = 1.0 y = 100000.0     print x/y 

My quotient displays as 1.00000e-05.

Is there any way to suppress scientific notation and make it display as 0.00001? I'm going to use the result as a string.

like image 291
Matt Avatar asked Mar 18 '09 15:03

Matt


People also ask

How do you print a float without scientific notation?

Within a given f-string, you can use the {...:f} format specifier to tell Python to use floating point notation for the number preceding the :f suffix. Thus, to print the number my_float = 0.00001 non-scientifically, use the expression print(f'{my_float:f}') .

How do you print a float in scientific notation?

`e' This prints a number in scientific (exponential) notation. For example, printf "%4.3e", 1950 prints `1.950e+03', with a total of four significant figures of which three follow the decimal point. The `4.3' are modifiers, discussed below. `f' This prints a number in floating point notation.


2 Answers

Using the newer version ''.format (also remember to specify how many digit after the . you wish to display, this depends on how small is the floating number). See this example:

>>> a = -7.1855143557448603e-17 >>> '{:f}'.format(a) '-0.000000' 

as shown above, default is 6 digits! This is not helpful for our case example, so instead we could use something like this:

>>> '{:.20f}'.format(a) '-0.00000000000000007186' 

Update

Starting in Python 3.6, this can be simplified with the new formatted string literal, as follows:

>>> f'{a:.20f}' '-0.00000000000000007186' 
like image 93
Aziz Alto Avatar answered Sep 22 '22 10:09

Aziz Alto


'%f' % (x/y) 

but you need to manage precision yourself. e.g.,

'%f' % (1/10**8) 

will display zeros only.
details are in the docs

Or for Python 3 the equivalent old formatting or the newer style formatting

like image 25
SilentGhost Avatar answered Sep 24 '22 10:09

SilentGhost