Python Format Number
Formatting numbers in Python is necessary to display numbers in a specific format.
Formatting can be used when you want to round off a number to a specific number of decimal places, or when you want your number to be separated by commas.
To format numbers we can use f-string or format() function.
- Round float to nearest integer
- Round float to 2 decimal places
- Format percentage value in numbers
- Format currency value in numbers
- Add comma separator in numbers
- Format numbers in scientific notation
- Format integer with leading zeros
- Conclusion
Table Of Contents
1. Round float to nearest integer
Let's say we have a float number and we want to round it off to the nearest integer.
We can do it in 3 different ways:
- Using format() function
- Using f-string
- Using round() function
To round off a float number to the nearest integer using format() or f-string use the format specifier {:.0f}.
num = 3.141592653589793
# π Method 1:
print("Nearest integer of num is {:.0f}".format(num))
# π Method 2:
print(f"Nearest integer of num is {num:.0f}")
# π Method 3:
print(f"Nearest integer of num is {round(num)}")
Nearest integer of num is 3 Nearest integer of num is 3 Nearest integer of num is 3
2. Round float to 2 decimal places
Suppose you want to round off a float number to 2 decimal places or any other number of decimal places.
To format the float numbers to 2 decimal places use the format specifier {:.2f} or {:.nf} for n decimal places in format() function or f-string.
num = 3.141592653589793
# π Method 1:
print("num round to 2 decimal is {:.2f}".format(num))
# π Method 2:
print(f"num round to 2 decimal is {num:.2f}")
num round to 2 decimal is 3.14 num round to 2 decimal is 3.14
3. Format percentage value in numbers
Format a percentage sign (%) after rounding float to 2 decimal places. For this, you can simply use the % sign after the format specifier in format() function or f-string.
The following example round off a float number to 2 decimal places and add a percentage sign after it.
num = 3.141592653589793
# π Method 1:
print("{:.2f}%".format(num))
# π Method 2:
print(f"{num:.2f}%")
3.14% 3.14%
4. Format currency value in numbers
To format an amount as a currency value, you can use the locale module.
locale module lets us work without having to worry about the conventions of a specific locale.
We can use this module to format currency values.
After importing it first set the locale to your country's locale. For example, locale.setlocale(locale.LC_ALL, 'en_US')
for US locale.
Then use locale.currency()
function to format the currency value.
import locale
amount = 525000
# set locale to US
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
# π format currency value
doller = locale.currency(amount, grouping=True)
print(doller)
# set locale to India
locale.setlocale(locale.LC_ALL, 'en_IN.UTF-8')
# π format currency value
rupee = locale.currency(amount, grouping=True)
print(rupee)
$525,000.00 βΉ5,25,000.00
5. Add comma separator in numbers
Comma separated numbers are easier to read. To add comma separator in numbers use the format specifier {:,} or {:,.nf} for n decimal places.
num = 1234567890.23456789
# π Method 1:
print("{:,}".format(num))
# π Method 2:
# comma separated with 2 decimal places
print(f"{num:,.2f}")
1,234,567,890.2345679 1,234,567,890.23
6. Format numbers in scientific notation
Scientific notation is a way of expressing numbers that are too large or too small to be conveniently written in decimal notation.
For example, 1,000,000,000 can be written as 1 x 109 and in scientific notation it is written as 1e9 or 1E9.
To format a number in scientific notation use the format specifier e or E.
num = 123456789
# π Method 1:
print("{:e}".format(num))
# π Method 2:
print(f"{num:E}")
Output:
1.234568e+09 1.234568E+09
7. Add leading zeros in numbers
Zeros are added to the left of the number to make it of a specific length.
Use the format specifier {0:>n} or {0:0n} to make the number of length n.
num = 123
# π Method 1:
print("{:0>6d}".format(num))
# π Method 2:
print(f"{num:06d}")
Output:
000123 000123
8. Format Number to Other Base
There are few format specifiers to format a number to other bases.
For example:
- :b for binary
- :o for octal
- :x for hexadecimal
num = 12345
# π binary Format
print(f"{num} in binary is {num:b}")
# π octal Format
print(f"{num} in octal is {num:o}")
# π hexadecimal Format
print(f"{num} in hexadecimal is {num:x}")
Output:
12345 in binary is 11000000111001 12345 in octal is 30071 12345 in hexadecimal is 3039
Conclusion
By the end of this article, you have learned all the ways to format numbers in Python. You need to command over format() function and f-string to format numbers so that you can format numbers in any way you want.
Use these formatting methods to make the numbers in your project more readable.
Happy coding! πβ¨