"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How Can I Suppress Scientific Notation When Printing Floating-Point Numbers in Python?

How Can I Suppress Scientific Notation When Printing Floating-Point Numbers in Python?

Posted on 2025-03-11
Browse:586

How Can I Suppress Scientific Notation When Printing Floating-Point Numbers in Python?

Suppressing Scientific Notation in Python

When working with floating-point values, you may encounter scientific notation (e.g., 1.00000e-05) when printing the results. This can be undesirable, especially if you need the result as a string with a specific format.

Problem:

Consider the following code:

x = 1.0
y = 100000.0    
print(x/y)

The quotient displays as "1.00000e-05," using scientific notation. You want to suppress this notation and display it as "0.00001" as a string.

Solution:

Using the ''.format() Method:

To suppress scientific notation using ''.format(), use the ".f" specifier. This specifier allows you to specify the number of decimal places to display. For example:

print("{:.5f}".format(x/y))

This will display the quotient as "0.00001."

Using Formatted String Literals (Python 3.6 ):

In Python 3.6 and later, you can use formatted string literals to simplify the process. The syntax is as follows:

print(f"{x/y:.5f}")

This achieves the same result as the ''.format() method.

Example:

x = 1.0
y = 100000.0
result = f"{x/y:.5f}"
print(result)

Output:

0.00001

By using these methods, you can suppress scientific notation when printing floating-point values and obtain the desired string format.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3