| by Arround The Web | No comments

Python Format Numbers

Python can format numbers to make them more readable and accurate. Formatting in Python means that we can add separators like commas to large numbers, such as “34,000,000” instead of “34000000” or round off decimals to a desired precision. In Python, there are multiple methods that are used for formatting numbers.

This guide will deliver an in-depth tutorial on Python format numbers utilizing:

Method 1: Format Number in Python Using the “str.format()” Method

The “str.format()” method can be used to format the number in Python. It can be used to format the decimal value into an integer or a specified number of places. To get started let’s understand this method using the below example code.

Example 1: Format Float as Integer, 2-Decimal Places, and as Percentage

Here is an example code that will take the specified float value as an argument and convert it into an integer using the “.0f” representation. The “.2f” expression is used to convert the float into specified decimal places. We can also format the given float into percentages using the format() method:

print('Format Float as Integer: ', '{:.0f}'.format(5.0))
print('\nFormat Float as Integer: ', '{:.0f}'.format(5.7))
print('\nFormat Float to 2-Decimal Places: ', '{:.2f}'.format(4.921312))
print('\nFormat Float as Percentage: ', '{:.2f}%'.format(123.125))

 

Here is the output of the above code:

Example 2: Add leading Zeros in Numbers

We can also add leading zeros in numbers by using the expression “:0>nd” in the format() method. In the below code, the leading zero is added to the input number:

num = 54567
print('Adding Leading Zeros: ', "{:0>6d}".format(num))

 

After executing the above code, the below output was retrieved:

Method 2: Format Number in Python Using “f-strings”

We can use the “f-strings” method to format the string in Python. This method can be used to format the number in Python, such as converting the float number into an integer, specifying decimal places, and using the separator of numbers like thousand. Let us see this method using the below example.

Example 1: Format Float as Integer, 2-Decimal Places, and as Thousand Separator

Here in this code, the input float number is converted into an integer, 2 decimal places, and with the thousand separators:

num = 54567.67897
print('Format Float as Integer: ', f"{num:.0f}")
print('\nFormat Float to 2-Decimal Places: ', f"{num:.2f}")
print('\nFormat Float With Thousands Separator: ', f"{num:,.2f}")

 

The given number has been formatted successfully:

Example 2: Add leading Zeros in Numbers

To add leading zeros in input numbers the “num:0nd” expression is passed inside the curly brackets prefix with “f”. Here, the “n” value is the full size which we need to fill with zeros:

num = 54567
print('Adding Leading Zeros: ', f"{num:06d}")

 

As you can see, the leading zero has been added:

Example 3: Representation of Numbers in Binary, Octal and Hexadecimal

We can also represent the input decimal number in binary, octal, and hexadecimal representation. In the below code, the “num:b”, “num:o”, and “num:x” expression is used to represent the input number in binary, octal, and hexadecimal:

num = 4523
print(f"Binary Representation of Input Number: {num:b}")
print(f"\nOctal Representation of Input Number: {num:o}")
print(f"\nHexadecimal Representation of Input Number: {num:x}")

 

The below code is used to represent the input number successfully:

Method 3: Format Number in Python Using the “%” Operator

The “%” operator is an old technique that can be used to format the numbers in Python. This method retrieves the integer and specifies decimal places when the input float value is passed:

num = 54567.67897
print('Format Float as Integer: ', "%.0f" % num)
print('\nFormat Float to 2-Decimal Places: ', "%.2f" % num)

 

The above code retrieves the below output:

Method 4: Format Number in Python Using the “round()” Method

The round() method can be used to format the number in Python. This method is used to round the float number into the nearest integer by truncating the decimal places. We can also specify the number of decimal places as a second argument:

num = 54567.67897
print('Format Float as Integer: ', round(num))
print('\nFormat Float to 2-Decimal Places: ', round(num, 2))

 

The below output shows the formatting of the float number:

Conclusion

The “str.format()” method, “f-strings” method, “%” operator, and “round()” methods are used to format the input numbers in Python. We can use these methods to format the float number into integers or into the specified decimal places. We can also add the leading zeros to the input number using the “format()” and “f-strings” methods. This tutorial delivered an in-depth analysis of formatting a Python number.

Share Button

Source: linuxhint.com

Leave a Reply