Python program to convert float to exponential


Float to exponential conversion involves transforming a decimal number, represented as a float, into exponential notation, also known as scientific notation. Exponential notation is commonly used for expressing very large or very small numbers in a concise manner, particularly in scientific and mathematical calculations.

On the other hand, a float is a data type used to store real numbers, including both whole and fractional numbers. In Python, floats are commonly used to represent floating-point numbers.

The process of converting a float to exponential can be achieved using various approaches in Python, such as using the format() function, f-string formatting, or the str.format() method. These methods allow us to format a float number in scientific notation by specifying the appropriate format specifier, such as 'e'.

Input Output Scenarios

To understand the process of converting a float to exponential notation, let's consider the following input-output scenarios −

Input: 1203100323.8932872122
Output: 1.203100e+09

The float number 1203100323.8932872122 is converted to exponential notation as 1.203100e+09. The "e+09" represents the exponent part of the number, indicating that the decimal point has been shifted nine places to the right.

Input: 0.0000123456789
Output: 1.23456789e-05

In this case, the float number 0.0000123456789 is converted to exponential notation as 1.23456789e-05. The "e-05" indicates that the decimal point has been shifted five places to the left.

Input: 9876543.21
Output: 9.87654321e+06

Here, the float number 9876543.21 is converted to exponential notation as 9.87654321e+06. The "e+06" signifies that the decimal point has been shifted six places to the right.

Let’s discuss the different approaches in Python.

Using the format() function

In this approach, we will be using the format() function with the "e" format specifier. 

Example

Here's an example using the format() function to convert a float to exponential notation in Python.

#define a function
def float_to_exponential(num):
    return format(num, "e")

# Define the floating point number
number = 12.1
print('Input floating point number:',number)

exponential = float_to_exponential(number)

# display the output exponential number 
print('Output exponential number:',exponential)

Output

Input floating point number: 12.1
Output exponential number: 1.210000e+01

Using the f-string formatting 

In this approach, we will use the:e format specifier to format the float number in exponential notation. The :e format specifier tells Python to display the number in scientific/exponential notation. 

Example

In this example, we will be using the f-string formatting to convert a float to exponential notation in Python.

#define a function
def float_to_exponential(num):
    return f"{num:e}"

# Define the floating point number
number = 10.01357
print('Input floating point number:',number)

exponential = float_to_exponential(number)

# display the output exponential number 
print('Output exponential number:',exponential)

Output

Input floating point number: 10.01357
Output exponential number: 1.001357e+01

Using the str.format() method

In this approach, we will use the str.format() method with the {:e} format specifier to format the float number in exponential notation.

Example

Here is an example, we will be using the str.format() method to convert a float to exponential notation.

#define a function
def float_to_exponential(num):
    return "{:e}".format(num)

# Define the floating point number
number = 0.64
print('Input floating point number:',number)

exponential = float_to_exponential(number)

# display the output exponential number 
print('Output exponential number:',exponential)

Output

Input floating point number: 0.64
Output exponential number: 6.400000e-01

These are the different approaches in Python to convert a float to exponential.

Updated on: 29-Aug-2023

991 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements