Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to convert float to exponential
Float to exponential conversion transforms a decimal number into scientific notation (e.g., 1.23e+05). This format is useful for expressing very large or small numbers concisely in scientific calculations.
Python provides three main approaches for converting floats to exponential notation: format() function, f-string formatting, and str.format() method. All use the 'e' format specifier to display numbers in scientific notation.
Input Output Examples
Here are common scenarios for float to exponential conversion ?
Input: 1203100323.8932872122 Output: 1.203100e+09 Input: 0.0000123456789 Output: 1.23456789e-05 Input: 9876543.21 Output: 9.87654321e+06
The exponential notation shows the coefficient (significant digits) and exponent (power of 10). Positive exponents indicate large numbers, negative exponents indicate small numbers.
Using format() Function
The format() function with the "e" specifier converts floats to exponential notation ?
def float_to_exponential(num):
return format(num, "e")
# Convert different float values
numbers = [12.1, 1203100323.8932872122, 0.0000123456789]
for number in numbers:
exponential = float_to_exponential(number)
print(f'Input: {number}')
print(f'Output: {exponential}')
print()
Input: 12.1 Output: 1.210000e+01 Input: 1203100323.8932872122 Output: 1.203100e+09 Input: 1.23456789e-05 Output: 1.234568e-05
Using F-String Formatting
F-strings with the :e format specifier provide a modern, readable approach ?
def float_to_exponential(num):
return f"{num:e}"
# Test with various numbers
test_values = [10.01357, 0.64, 9876543.21]
for number in test_values:
exponential = float_to_exponential(number)
print(f'Input: {number} ? Output: {exponential}')
Input: 10.01357 ? Output: 1.001357e+01 Input: 0.64 ? Output: 6.400000e-01 Input: 9876543.21 ? Output: 9.876543e+06
Using str.format() Method
The str.format() method with {:e} specifier offers another formatting option ?
def float_to_exponential(num):
return "{:e}".format(num)
# Convert and display results
numbers = [0.64, 123456.789, 0.000001]
for number in numbers:
exponential = float_to_exponential(number)
print('Input floating point number:', number)
print('Output exponential number:', exponential)
print('-' * 40)
Input floating point number: 0.64 Output exponential number: 6.400000e-01 ---------------------------------------- Input floating point number: 123456.789 Output exponential number: 1.234568e+05 ---------------------------------------- Input floating point number: 1e-06 Output exponential number: 1.000000e-06
Controlling Precision
You can control the number of decimal places in exponential notation ?
number = 123.456789
# Different precision levels
print(f'Default: {number:e}')
print(f'2 decimals: {number:.2e}')
print(f'4 decimals: {number:.4e}')
print(f'No decimals: {number:.0e}')
Default: 1.234568e+02 2 decimals: 1.23e+02 4 decimals: 1.2346e+02 No decimals: 1e+02
Comparison of Methods
| Method | Syntax | Python Version | Best For |
|---|---|---|---|
format() |
format(num, "e") |
All versions | Simple conversion |
| F-string | f"{num:e}" |
3.6+ | Modern, readable code |
str.format() |
"{:e}".format(num) |
2.7+ | Template strings |
Conclusion
Use f-strings for modern Python projects, format() for simple conversions, and str.format() for template-based formatting. All methods support precision control with format specifiers like .2e for two decimal places.
