Scientific notation is Python's default way of displaying very large or very small floating-point numbers. A value like 510000000.0 might appear as 5.1e+08, and 0.00000032 becomes 3.2e-07. While this representation is compact and mathematically precise, it can confuse end users in reports, dashboards, financial statements, and exported data files.
Python provides a rich set of formatting tools that let you control exactly how numbers are displayed. This guide covers how to convert scientific notation to standard decimal format, how to control precision, how to suppress scientific notation in Pandas and NumPy, and how to handle high-precision requirements with the decimal module.
Understanding When Python Uses Scientific Notation
Python automatically switches to scientific notation when floating-point numbers are very large or very small:
large_number = 510000000.0
small_number = 0.00000032
print(large_number)
print(small_number)
huge = 1e20
print(huge)Output:
510000000.0
3.2e-07
1e+20Notice that 510000000.0 is still displayed in standard form because Python considers it readable enough, while 0.00000032 and 1e20 are converted to scientific notation automatically. The threshold is not a fixed rule but depends on the number of significant digits and overall magnitude.
Converting Scientific Notation to Standard Decimal Format
To display a number in standard decimal form regardless of its magnitude, use the :f format specifier inside an f-string:
value = 4.25e+5
formatted = f"{value:.2f}"
print(f"Original: {value}")
print(f"Formatted: {formatted}")Output:
Original: 425000.0
Formatted: 425000.00This works equally well for very small numbers:
tiny = 3.7e-4
print(f"Default: {tiny}")
print(f"Fixed (6): {tiny:.6f}")
print(f"Fixed (10): {tiny:.10f}")Output:
Default: 0.00037
Fixed (6): 0.000370
Fixed (10): 0.0003700000The number after the dot controls how many decimal places are shown. The :f specifier forces fixed-point notation and never falls back to scientific notation.
Controlling Decimal Precision
F-string format specifiers give you fine-grained control over how many decimal places appear in the output:
number = 1234567.89123
print(f"No decimals: {number:.0f}")
print(f"Two decimals: {number:.2f}")
print(f"Five decimals: {number:.5f}")Output:
No decimals: 1234568
Two decimals: 1234567.89
Five decimals: 1234567.89123Adding Thousand Separators
For large numbers, adding commas or underscores as thousand separators dramatically improves readability:
number = 1234567.89123
print(f"With commas: {number:,.2f}")
print(f"With underscores: {number:_.2f}")Output:
With commas: 1,234,567.89
With underscores: 1_234_567.89Format Specifier Reference
Here is a quick reference for the most commonly used format specifiers:
| Specifier | Purpose | Example Input | Output |
|---|---|---|---|
:e |
Force scientific notation | 1500.0 |
1.500000e+03 |
:.2e |
Scientific with 2 decimals | 1500.0 |
1.50e+03 |
:f |
Force fixed-point decimal | 1.5e3 |
1500.000000 |
:.2f |
Fixed-point with 2 decimals | 1.5e3 |
1500.00 |
:g |
Adaptive (auto-selects best) | 1500.0 |
1500 |
:.4g |
Adaptive with 4 significant digits | 1500.0 |
1500 |
:,.2f |
Fixed-point with commas | 1500000.0 |
1,500,000.00 |
Suppressing Scientific Notation in Pandas
Data scientists frequently encounter unwanted scientific notation when displaying DataFrames. Pandas uses scientific notation by default for very large or very small float values, which can make financial and analytical reports hard to read.
Global Configuration
You can suppress scientific notation globally for all DataFrame displays:
import pandas as pd
df = pd.DataFrame({
"revenue": [1.5e8, 2.3e7, 9.8e6],
"conversion_rate": [1.2e-3, 3.4e-4, 5.6e-5],
})
print("Default:")
print(df)
print()
pd.options.display.float_format = "{:.2f}".format
print("Formatted:")
print(df)Output:
Default:
revenue conversion_rate
0 150000000.0 0.001200
1 23000000.0 0.000340
2 9800000.0 0.000056
Formatted:
revenue conversion_rate
0 150000000.00 0.00
1 23000000.00 0.00
2 9800000.00 0.00Per-Column Formatting
The global setting applies the same format to all float columns, which can be problematic when different columns need different precision. For more granular control, format specific columns individually:
import pandas as pd
pd.reset_option("display.float_format")
df = pd.DataFrame({
"revenue": [1.5e8, 2.3e7, 9.8e6],
"conversion_rate": [1.2e-3, 3.4e-4, 5.6e-5],
})
df["revenue"] = df["revenue"].apply(lambda x: f"{x:,.0f}")
df["conversion_rate"] = df["conversion_rate"].apply(lambda x: f"{x:.4%}")
print(df)Output:
revenue conversion_rate
0 150,000,000 0.1200%
1 23,000,000 0.0340%
2 9,800,000 0.0056%Controlling Scientific Notation in NumPy
NumPy arrays also default to scientific notation for extreme values. Use np.set_printoptions() to control the display:
import numpy as np
arr = np.array([1.5e8, 2.3e-5, 9.8e6])
print("Default:")
print(arr)
np.set_printoptions(suppress=True, precision=2)
print("\nSuppressed:")
print(arr)
np.set_printoptions(suppress=False)Output:
Default:
[1.5e+08 2.3e-05 9.8e+06]
Suppressed:
[1.5e+08 2.3e-05 9.8e+06]Handling High-Precision Requirements with the Decimal Module
Standard Python floats are 64-bit IEEE 754 double-precision numbers, which means they can only represent approximately 15 to 17 significant digits reliably. Beyond that, precision is silently lost:
large_precise = 12345678901234567890.12345
print(f"Float: {large_precise:.5f}")Output:
Float: 12345678901234567168.00000The last few digits are wrong because the number exceeds what a 64-bit float can represent exactly. For financial calculations, scientific computing, or any application that requires exact decimal representation, use the decimal module:
from decimal import Decimal, getcontext
getcontext().prec = 30
precise = Decimal("12345678901234567890.12345")
print(f"Decimal: {precise}")
result = precise * Decimal("2")
print(f"Doubled: {result}")Output:
Decimal: 12345678901234567890.12345
Doubled: 24691357802469135780.24690Summary
Python gives you full control over how numbers are displayed, regardless of how they are stored internally. The key tools are:
:.Nfformat specifier to force fixed-point decimal output with a specific number of decimal places.:gformat specifier to let Python choose the most readable format automatically.:,.Nfformat specifier to add thousand separators for improved readability of large numbers.pd.options.display.float_formatto suppress scientific notation globally in Pandas DataFrames.np.set_printoptions(suppress=True)to suppress scientific notation in NumPy array output.decimal.Decimalfor applications that require exact decimal precision beyond what 64-bit floats can provide.
By choosing the right formatting approach for your context, you ensure that numerical data remains clear and professional whether it appears in a scientific report, a financial dashboard, or a user-facing application.