Format a Floating Number to String in Python

Ankur Baral Jan 30, 2023 Jun 15, 2021
  1. Format Floating Numbers to a Fixed Width Using Format Specification and the format() Method
  2. Format Floating Numbers in a List to a Fixed Width
  3. Format a Floating Number to Fix Width Using the % Operator in Python
  4. Format a Floating Number to a Fix Width Using round() Function in Python
Format a Floating Number to String in Python

This article will introduce some methods to format a floating number to a fixed width in Python.

Format Floating Numbers to a Fixed Width Using Format Specification and the format() Method

Here, we will use the normal formatting process via format specification to fix the width of floating numbers.

We will create a variable num and assign a floating number to it. Then, we will print the floating number in the format we need. We are going to use the format() function to get our result.

Example Codes:

#python 3.x
num = 0.02893574
print ("{:.4f}".format(num))

Output:

0.0289

Here, .4f is called format specification, which denotes that the output should show only four places after the decimal. If we used .5f instead, we’d get up to five digits after the decimal point. The empty string before the colon : is placed so that the argument provided to the format() function takes that place. In the above program, the argument we have provided is num. So, whatever value we have in num will be passed to the empty string before the : and formatted accordingly.

In python 3.6, we can also use f'{}' to obtain the same output:

#python 3.x
num = 0.02893574
print (f'{num:.4f}')

Output:

0.0289

Format Floating Numbers in a List to a Fixed Width

We use similar syntaxes to fix the width of floating numbers up to a specific digit after the decimal.

First, we will create a list with several floating numbers. Then, we will use for loop to take every item from the list and format it accordingly. We will use the variable numbers to represent numbers inside the list. We will print the floating numbers in the list one by one with fixed numbers of digits after the decimal.

Example Code:

#python 3.x
list = [18.292164, 52.452189, 999.1212732]
for numbers in list:
    print("{:.3f}".format(numbers))

Output:

18.292
52.452
999.121

In the above code, each number inside the list is sent one by one inside the for loop. The first element of the list, i.e., list[0], gets assigned to the variable numbers, and its formatted value, i.e., 18.293, is printed. It happened because we executed .3f, which represents digits up to three places after the decimal point. Similarly, the second element list[1] and the third list[2] is also passed to the loop as variable numbers and are printed accordingly.

This program runs until all the elements in the list are executed.

Using the f'{}':

Example Code:

The list below contains the same items as the program above. We will use f'{}' instead of the format() function in this example.

#python 3.x
list = [18.292164, 52.452189, 999.1212732]
for numbers in list:
    print(f'{numbers:9.3f}')

Output:

 18.292
 52.452
999.121

We can see that when we got the output when we used f'{}'. We also got the desired output in an aligned manner. For the same reason, it is generally better to use f'{}' if we want to have the floating digits after the decimal aligned.

Format a Floating Number to Fix Width Using the % Operator in Python

We can also set a fixed width for a floating number with the use of %v operator. The code might look similar to the printf() function in C programming.

We will assign a floating number to a variable num and print the value of num with decimal digits up to a fixed width. Notice that while passing the value of num to the print statement with format specifier %.4f, we are using %num. Missing % before num will be a syntax error.

Example Code:

#python 3.x
num = 0.02893574
print ('%.4f'%num)

Output:

0.0289

Here the use of %num has allowed us to print the desired value without any function or string formatting.

Format a Floating Number to a Fix Width Using round() Function in Python

We can also use the round() function to fix the numbers of digits after the decimal point. This function limits the number of digits after the decimal point on the input number. It also rounds off the digit at which limit is set to its upper integral value if the digit is greater than value 5.

Let’s take a floating-point number and assign it to a variable num. When we print, we will use the round() function to limit the number of digits after the decimal point.

Example Code:

#python 3.x
num = 2.37682
print(round(num,3))

Output:

2.377

The syntax for the round function is round(number, digits). Here the argument number is compulsory while the argument digits is optional. number can also be put as a variable. If nothing is passed to the argument digits, only the integral part of the number is taken as a result.

In the above program, we passed the value 2.37682 to the first argument num, and it was rounded to 3 places after the decimal point because we passed the value 3 to the second argument inside the round() method.

Related Article - Python Float