Categories
Python Answers

How to display a decimal in scientific notation with Python?

Spread the love

Sometimes, we want to display a decimal in scientific notation with Python.

In this article, we’ll look at how to display a decimal in scientific notation with Python.

How to display a decimal in scientific notation with Python?

To display a decimal in scientific notation with Python, we can use the %.2E format string.

For instance, we write:

from decimal import Decimal

print('%.2E' % Decimal('40800000000.00000000000000'))

We create a decimal number object with the Decimal constructor and a number string.

Then we use %.2E to return a string with the number formatted into scientific notation.

Therefore, print prints '4.08E+10'.

Conclusion

To display a decimal in scientific notation with Python, we can use the %.2E format string.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *