Table of Contents

## Problem Formulation

If you print a float value in Python that is smaller than `0.0001`

, Python will use the scientific notation for small numbers such as `1e-05`

that is short for `1*1/10**-5`

.

Here’s an example output when printing smaller and smaller floats to the shell. If there are more than three zeros after the decimal point, Python will use the scientific notation:

>>> print(0.01) 0.01 >>> print(0.001) 0.001 >>> print(0.0001) 0.0001 >>> print(0.00001) 1e-05 >>> print(0.000001) 1e-06

**Problem**: How to display the numbers non-scientifically?

Here’s your *desired *output:

>>> print(0.00001) 0.00001

## Solution 1: f-String Formatting

The most powerful way is to use Python’s capabilities for string formatting with f-strings, i.e., special types of formatting string that are enclosed in `f'...'`

. Within a given f-string, you can use the `{...:f}`

format specifier to tell Python to use floating point notation for the number preceding the `:f`

suffix. Thus, to print the number `my_float = 0.00001`

non-scientifically, use the expression `print(f'{my_float:f}')`

.

my_float = 0.00001 print(f'{my_float:f}') # 0.000010

The problem with this approach is that if you try it for smaller numbers, it only display `'0'`

digits because the default precision is constant:

my_float = 0.0000001 print(f'{my_float:f}') # 0.000000

The default :f precision generates a string with nine characters (including floating point). If the float value cannot be represented within nine characters, the string representation of the float will be cut off. This is clearly not what you want!

## Solution 2: f-String Formatting with Precision

The best way to print even small float numbers is to use f-strings, i.e., special types of formatting string that are enclosed in `f'...'`

. Within a given f-string, you can use the `{...:.12f}`

format specifier to tell Python to use floating point precision with 12 digits after the decimal point. Thus, to print the number `my_float = 0.0000001`

non-scientifically with 14 decimal digits, use the expression `print(f'{my_float:.14f}')`

.

my_float = 0.00001 print(f'{my_float:.14f}') # 0.00001000000000

And with 8 digits:

my_float = 0.00001 print(f'{my_float:.8f}') # 0.00001000

Feel free to watch my short video tutorial on f-strings and the alternative `string.format()`

function here:

However, if you want to automatically suppress trailing `'0'`

digits, this method won’t help:

## Solution 3: NumPy format_float_positional()

The `np.format_float_positional()`

method formats a given float as a decimal string using the non-scientific positional notation. The first argument is the float to be displayed. The optional `trim`

argument when set to `'-'`

trims trailing zeros and any trailing decimal point if not needed.

Here’s a simple example:

import numpy as np my_float = 0.00001 print(np.format_float_positional(my_float, trim='-')) # 0.00001

Let’s apply this method to multiple numbers to see that NumPy automatically trims all trailing zeros. For convenience, I’ve defined a simple conversion function using the lambda notation:

>>> pretty_print = lambda x: np.format_float_positional(x, trim='-') >>> pretty_print(0.001) '0.001' >>> pretty_print(0.0001) '0.0001' >>> pretty_print(0.00001) '0.00001' >>> pretty_print(0.000001) '0.000001' >>> pretty_print(0.0000001) '0.0000001' >>> pretty_print(0.00000001) '0.00000001'

You can watch the lambda function video introduction here:

## Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation. To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

**You build high-value coding skills by working on practical coding projects!**

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

If your answer is ** YES!**, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.