Skip to main content

How to Convert and Format Scientific Notation in Python

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+20

Notice 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.00

This 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.0003700000

The 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.

Automatic String Parsing

Python's float() function automatically recognizes scientific notation in strings. For example, float("1.5e3") returns 1500.0. Format specifiers only affect how numbers are displayed, not how they are stored or calculated internally.

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.89123
note

Notice that when using .0f, the number is rounded to the nearest integer. Python follows standard rounding rules when truncating decimal places.

Adding 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.89

Format Specifier Reference

Here is a quick reference for the most commonly used format specifiers:

SpecifierPurposeExample InputOutput
:eForce scientific notation1500.01.500000e+03
:.2eScientific with 2 decimals1500.01.50e+03
:fForce fixed-point decimal1.5e31500.000000
:.2fFixed-point with 2 decimals1.5e31500.00
:gAdaptive (auto-selects best)1500.01500
:.4gAdaptive with 4 significant digits1500.01500
:,.2fFixed-point with commas1500000.01,500,000.00
The Adaptive :g Format

When you are not sure which format best suits your data, use :g. Python automatically chooses between standard and scientific notation based on the magnitude of the number, and it removes unnecessary trailing zeros for cleaner output:

print(f"{1500.0:g}")       # 1500
print(f"{0.00037:g}") # 0.00037
print(f"{1e20:g}") # 1e+20
print(f"{1500.10000:g}") # 1500.1

Output:

1500
0.00037
1e+20
1500.1

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],
})

# Default display uses scientific notation
print("Default:")
print(df)
print()

# Suppress scientific notation globally
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.00

Per-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

# Reset global formatting to default
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],
})

# Format each column with appropriate precision
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%
caution

Applying .apply(lambda x: f"{x:...}") converts the column from numeric floats to strings. This is fine for display and export, but you will not be able to perform mathematical operations on those columns afterward. Do this as a final formatting step before displaying or exporting your data.

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)

# Suppress scientific notation and set precision
np.set_printoptions(suppress=True, precision=2)

print("\nSuppressed:")
print(arr)

# Reset to defaults when done
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]
note

Notice that the very small value 2.3e-05 appears as 0. when precision is set to 2, because the significant digits fall beyond the displayed decimal places. Increase the precision if you need to see small values accurately:

np.set_printoptions(suppress=True, precision=8)
print(arr)

Output:

[150000000.      0.000023  9800000.  ]

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.00000

The 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

# Set precision high enough for your needs
getcontext().prec = 30

precise = Decimal("12345678901234567890.12345")
print(f"Decimal: {precise}")

# Arithmetic stays precise
result = precise * Decimal("2")
print(f"Doubled: {result}")

Output:

Decimal: 12345678901234567890.12345
Doubled: 24691357802469135780.24690
Floating-Point Precision Limits

Standard Python floats can only reliably represent 15 to 17 significant digits. For financial calculations, currency handling, or any application where exact decimal values matter, use the decimal module. Subtle rounding errors from float arithmetic can accumulate and produce incorrect results in production systems.

Summary

Python gives you full control over how numbers are displayed, regardless of how they are stored internally. The key tools are:

  • :.Nf format specifier to force fixed-point decimal output with a specific number of decimal places.
  • :g format specifier to let Python choose the most readable format automatically.
  • :,.Nf format specifier to add thousand separators for improved readability of large numbers.
  • pd.options.display.float_format to suppress scientific notation globally in Pandas DataFrames.
  • np.set_printoptions(suppress=True) to suppress scientific notation in NumPy array output.
  • decimal.Decimal for 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.