AppDividend
Latest Code Tutorials

How to Format Float Values in Python

Formatting floating-point numbers by specifying the precision helps organize data visually. To format numbers in Python, our go-to method is a string.format(), including float and integer formatting. Let’s see how to format float in Python in detail.

Python format float

To format float values in Python, use the format() method. The format() is a built-in Python formatting method that allows multiple substitutions and value formatting.

Let’s take an example.

x = 211911461911461819112146
y = 2**70

print(x / y)

Output

179.4959901398329

To format the float value up to two decimal places, use the %.2f.

x = 211911461911461819112146
y = 2**70
z = x / y

print("%.2f" % z)

Output

179.50

To format up to three decimal places, use the %.3f.

x = 211911461911461819112146
y = 2**70
z = x / y

print("%.3f" % z)

Output

179.496

You can see that when we are printing only two decimal places, it automatically rounds to the nearest integer. Still, printing a number up to three decimal places does not go round to the nearest integer. The 0.496 prints as it is since we want a floating value up to three decimal places.

The computers are binary, so they store floating-point numbers as an integer and then divide it by a power of two and print the value as it is from memory.

Formatting the output should allow you to add padding as required, as well as add comma separators, etc.

Using format() method

The “Format specifications” are used within replacement fields contained within a format string to define how individual values are presented.

See the below code.

x = 211911461911461819112146
y = 2**70
z = x / y

print("{:.2f}".format(z))

Output

179.50

The output returns a string. To get the output as float, use the float() function.

x = 211911461911461819112146
y = 2**70
z = x / y

print(float("{:.2f}".format(z)))

Output

179.5

The wrapping with float() method doesn’t change anything except removing the 0 after the decimal point.

Format float value using the round() Method in Python

The round() is a built-in Python method that returns the floating-point number rounded off to the given digits after the decimal point. You can use the round() method to format the float value.

x = 211911461911461819112146
y = 2**70
z = x / y

print(round(z, 2))

Output

179.5

But please note that the behavior of the round() function for floats can be surprising in some cases. For example, round(3.575, 2) gives 3.57 instead of the expected 3.58. This is not a bug: it’s a result that most decimal fractions can’t be represented exactly as a float.

Python f-strings

Python f String is an improvement over previous formatting methods. You can read more at PEP8.

Let’s use the f-strings to format the float value.

x = 211911461911461819112146
y = 2**70
z = x / y

print(f'{z:.2f}')

Output

179.50

It works well with long calculations with operators and does not need parenthesis.

Python float format: Complete Table

Number Format Output Description
3.1415926 {:.2f} 3.14 Format float 2 decimal places
3.1415926 {:+.2f} +3.14 Format float 2 decimal places with sign
-1 {:+.2f} -1.00 Format float 2 decimal places with sign
2.71828 {:.0f} 3 Format float with no decimal places
4 {:0>2d} 04 Pad number with zeros (left padding, width 2)
4 {:x<4d} 4xxx Pad number with x’s (right padding, width 4)
10 {:x<4d} 10xx Pad number with x’s (right padding, width 4)
1000000 {:,} 1,000,000 Number format with comma separator
0.35 {:.2%} 35.00% Format percentage
1000000000 {:.2e} 1.00e+09 Exponent notation
11 {:11d}         11 Right-aligned (default, width 10)
11 {:<11d} 11 Left-aligned (width 10)
11 {:^11d}     11 Center aligned (width 10)

Conclusion

Most numbers in Python cannot be accurately represented in floats. If you need to round the number because that’s what your mathematical formula or algorithm requires, then you should use the round() method.

If you want to restrict the display to a certain precision, don’t even use round and format the string using %.2f or f-strings or format based on your requirements.

Related posts

Python format strings

Python 0

Python f-strings

Python string format

Python time format

1 Comment
  1. Justin says

    Thank you this was really helpful.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.