AppDividend
Latest Code Tutorials

How to Format Float Values in Python

0

Formatting floating-point numbers by specifying the precision helps organize data visually. To format numbers in Python, our go-to method is string.format() including both float formatting 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. Python format() is a built-in 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, when printing a number up to three decimal places, it 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 adding 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 it removes the 0 after the decimal point.

Format float value using 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 one thing to 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 of the fact 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 too, with operators and not needing parenthesis.

Python float format: Complete Table

NumberFormatOutputDescription
3.1415926{:.2f}3.14Format float 2 decimal places
3.1415926{:+.2f}+3.14Format float 2 decimal places with sign
-1{:+.2f}-1.00Format float 2 decimal places with sign
2.71828{:.0f}3Format float with no decimal places
4{:0>2d}04Pad number with zeros (left padding, width 2)
4{:x<4d}4xxxPad number with x’s (right padding, width 4)
10{:x<4d}10xxPad number with x’s (right padding, width 4)
1000000{:,}1,000,000Number format with comma separator
0.35{:.2%}35.00%Format percentage
1000000000{:.2e}1.00e+09Exponent notation
11{:11d}        11Right aligned (default, width 10)
11{:<11d}11Left aligned (width 10)
11{:^11d}    11Center 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 just want to restrict the display to a certain precision, then don’t even use round and just format the string using %.2f or f-strings or format based on your requirements.

Leave A Reply

Your email address will not be published.

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