Skip to content

Let python3 output numbers as scientific notation

To format numbers as scientific notation in Python 3, you can use the f-string formatting or the format() method. Here’s how you can do it:

Using f-string

number = 12345678.9
formatted_number = f"{number:.2e}"
print(formatted_number)  # Output: 1.23e+07

Using format() method

number = 12345678.9
formatted_number = "{:.2e}".format(number)
print(formatted_number)  # Output: 1.23e+07

In both examples, the :.2e format specifier represents scientific notation with 2 decimal places. You can adjust the precision (number of decimal places) as needed. Replace 2 with your desired precision.

Leave a Reply