# How to Suppress Scientific Notation in Python

[toc]

Summary: Use the string literal syntax `f"{number:.nf}"` to suppress the scientific notation of a number to its floating-point representation.

Problem Formulation: How will you suppress a number represented in scientific notation by default to a floating-point value?

Note: Generally, Python represents huge floating-point numbers or very small floating-point numbers in their scientific form. Scientific notation of a number represents the number in terms of powers of 10. For example, the scientific notation for the number `0.0000025` is `2.5e-6`.

Example: Consider the following code snippet.

```number = 2.5 / 1000000
print(number)```

The output is `2.5e-06`. How will you suppress the scientific notation and display the output as `0.000025`?

## Solution 1: Using String Literal (f-string)

β¨Approach: If you are using Python 3.6 or above, then the simplest solution to our problem is to use the string literal syntax `f"{number:.nf}"` to represent the given floating-point number in its decimal format with `n` places of decimal points.

Code:

```number = 2.5 / 1000000
print(f"Scientific Notation: {number}")
print(f"Decimal Representation: {number:.7f}")```

Output:

```Scientific Notation: 2.5e-06
Decimal Representation: 0.0000025```

TRIVIA: The string literal technique relies upon `str.format()` implicitly.

## Solution 2: Using str.format()

`format()` is a built-in function in Python that returns a formatted string. It formats the specific values and inserts them in the placeholder of the string.

β¨Approach: To represent the scientific notation of the output in decimal format with `n` places following the decimal point, use `str.format(number)` where `str` is “`{:.nf}`” .

Code:

```number = 2.5 / 1000000
print("Scientific Notation: ", number)
print("Decimal Representation: {:.7f}".format(number))```

Output:

```Scientific Notation:  2.5e-06
Decimal Representation: 0.0000025```

Note: Ensure that you manage the value of `n`, that is, the number after the decimal points you want to display properly, depending on how small the floating-point number is. For example, have a look at the following example:

```a = -7.1855143557448603e-17
print('{:f}'.format(a))

# -0.000000```

Since the default number of digits after the decimal point is 6, the output of the above snippet will be `-0.000000`. This is not helpful for the given example. Hence, we could use something which looks like this:

```a = -7.1855143557448603e-17
print('{:.30f}'.format(a))

# OUTPUT: -0.000000000000000071855143557449```

Recommended Tutorials to understand how string formatting works in Python:

## Solution 3: Using % Formatting

The % operator allows you to format a string object in Python. Conversion specifiers, like `%f` and` %s`, are used in the format string as placeholders. These specifiers dictate how % will format the values.

β¨Approach: If you are using an older version of Python, use `%.nf` to suppress the scientific notation to a floating-point value upto `n` decimal places. Remember to manage the precision manually.

Code:

```number = 2.5 / 1000000
print("Scientific Notation: ", number)
print("Decimal Representation: %.7f" % number)```

Output:

```Scientific Notation:  2.5e-06
Decimal Representation: 0.0000025```

## Solution 4: Using Pandas

β¨Approach: Another option, if you are using pandas and would like to suppress scientific notation for all floats, is to adjust Pandas options as shown below.

Code:

```import pandas as pd

pd.options.display.float_format = '{:.10f}'.format
df = pd.DataFrame([1.192433e-06, 1.293066e-06, 1.077142e-06], index=['foo', 'bar', 'baz'], columns=['time'])
print(df)```

Output:

```            time
foo 0.0000011924
bar 0.0000012931
baz 0.0000010771```

Learn Pandas the Fun Way by Solving Code Puzzles

• If you want to boost your Pandas skills, consider checking out my puzzle-based learning book Coffee Break Pandas (Amazon Link).

## Solution 5: Using Numpy

In the above approaches, we had to specify the precision manually. But, what if you do not have an idea about the precision? This is where the `Numpy` library will help you.

β¨Approach: `format_float_positional()` is a function of the Numpy library in Python that formats a floating-point scalar. It then returns the decimal string representation of the floating-point value passed. The optional `trim` argument, when set to `'-'` trims trailing zeros and any trailing decimal point if not needed.

Code:

```import numpy as np
number = 2.5 / 1000000
print("Scientific Notation: ", number)
print("Decimal Representation: ", np.format_float_positional(number, trim='-'))```

Output:

```Scientific Notation:  2.5e-06
Decimal Representation:  0.0000025```

Do you want to become a NumPy master? Check out our interactive puzzle book Coffee Break NumPy and boost your data science skills! (Amazon link opens in new tab.)

## Solution 6: Using decimal Module

Another workaround to the given problem is to import the decimal module and use its Decimal instance, as shown below.

```from decimal import Decimal
number = str(Decimal(2.5) / Decimal(1000000))
print(number)```

Output:

`0.0000025`

β οΈCaution: This approach won’t work for values smaller than `1e-6`. It turns values lesser than `1e-6` to their scientific notation.

### β¨How to Print a Number in Scientific Notation in Python?

You can convert a number to its scientific notation using `"{:e}".format(num)`. To specify the precision, i.e., the number of digits after the decimal point, use `"{:.Ne}"`, where `N` represents the number of digits.

Example:

```num = 1250000000
# format number into scientific notation
print("{:e}".format(num))
# format scientific notation with 2 digits
print("{:.2e}".format(num))```

Output:

```1.250000e+09
1.25e+09```

### β¨How to Limit Floats to Two Decimal Points in Python?

Use the built-in function `round()` to round off the given decimal number to the given precision.

Example:

```x = 3.14667
y = round(x)
z = round(x, 3)  # Limit the float value to three decimal points
print("x=", x)
print("y=", y)
print("z=", z)```

Output:

```x= 3.14667
y= 3
z= 3.147```

Read Here: How to Limit Floats to Two Decimal Points in Python?

### β¨How to Match Scientific Notation in a String?

Leverage the power of regex to detect the presence of numbers represented in their scientific notation in a given string.

Example:

```import re

val = "the scientific notation for the number 0.0000025 is 2.5e-6"
num = re.compile('[-+]?[\d]+\.?[\d]*[Ee](?:[-+]?[\d]+)?')
print(re.findall(num, val))```

Output:

`['2.5e-6']`

Google, Facebook, and Amazon engineers are regular expression masters. If you want to become one as well, check out our new book: The Smartest Way to Learn Python Regex (Amazon Kindle/Print, opens in new tab).

## Conclusion

To wrap things up, if you are on a newer version of Python, then it is advisable to use the string literal approach to suppress the scientific notation to a floating-point value. In case you are dealing with a Pandas dataframe or a Numpy array, then proposed solutions 4 and 5 are worth the try.

With that, we come to the end of this tutorial. Please subscribe and stay tuned for more interesting tutorials in the future. Happy learning!