DEV Community

Samiur
Samiur

Posted on • Originally published at kodyoon.com

Python convert scientific notation string to float

Python convert scientific notation string to float

In this tutorial let us understand how to yield a number of a specific type.

Python supplies three different numeric types say int, float and complex.

  • int represent Integers. booleans are sub types of integers. Integers have unlimited precision.
  • float refers to the floating point numbers. A number containing a . or an exponential sign refer to a floating point number.
  • complex numbers have a real and imaginary part. If n is a complex number then n.real extracts the real part of n and n.imaginary extracts the imaginary part of n. A complex value is represented as x + yj appending j or J to the numeric literal yields an imaginary number.

The following constructors are used to produce an integer, a floating point and a complex number respectively - int(), float(), complex()

int(x) -> x is converted to integer
float(x) -> x is converted to a floating point
complex(x, y) -> converted to a complex number with x as a real value and y as an imaginary value.

Let us consider a simple code:

n = 34.56;
print("integer number : ", int(n));

output:

PS C:\PycharmProjects\pythonProject> python sample.py
integer number : 34

Scientific numbers are the special way of writing numbers in powers of 10. for example:

700 = 7 100 = 7 10^2 is the scientific notation of number 700

Let us consider a bigger number like,

6,300,000,000 = 63 10^8 or 6.3 10^9 so, this is the scientific notation of number 6,300,000,000.

Oftentimes e or E are used to represent the 10. Hence 6.3 e+09 is equivalent to 6.3 10^9

As we discussed about the data conversions above, float() constructor can be used to adorn a scientific notation string. Let us understand with a simple example:

n = 1.230000e+07;
print("integer number : ", float(n));

Now, let us first calculate this manually

1.230000* 10 ^ 7 => 1230000 an integer , because the conversion is a floating pint number the result of this code should be 1230000.0

PS C:\PycharmProjects\pythonProject> python sample.py
floating point number : 12300000.0

for more details

Latest comments (0)