Print floats to a specific number of decimal points in Python

The float() function allows the user to convert a given value into a floating-point number. In this tutorial, you will learn how to convert a number into a floating-point number having a specific number of decimal points in Python programming language.

Syntax of float in Python

float(value)

In the above code, the given the float() is a built-in function having a single parameter which can contain any numerical value.

Example:

a = 12    #Assigning the value 12 to the variable "a"
print(a)  
print(float(a))

Output:
12
12.0

From the above code, we can see that the difference in the outputs when we print the variable “a” and when we print the variable “a” using the float() function. We can also notice that by default the float() function prints the value only to one decimal point.

Now that we know the basic functioning of how the float() function works let us now explore the various methods to convert a specific number of decimal points.

format specifier “%” to print floats to a specific number of decimal points in Python

The method can be best explained with an example given below:

Example:

x= 4.56745    #Initialising the variable "x"

# using the format specifier "%" to print value till 2 decimal places
print("%.2f" % x)

# using the format specifier "%" to print value till 2 decimal places
print("%.3f" % x)

# using the format specifier "%" to print value till 2 decimal places
print("%.4f" % x)

Output:
4.57
4.567
4.5675

From the above code, we can see that “%.2f” prints the value up to 2 decimal places i.e, basically rounding off the value of “x”.

The same principle holds true for “%.3f” and “%.4f” as well and hence can be concluded that the format specifier “%f” can be used to convert a specific number of decimal points.

format() function to print floats to a specific number of decimal points in Python

The method can be best explained with an example given below:

Example:

a = 20    #Initialising the variable "a"

# using the format() function to print value till 2 decimal places
print("{0:.2f}".format(a)) 

# using the format() function to print value till 3 decimal places
print("{0:.3f}".format(a))

# using the format() function to print value till 4 decimal places
print("{0:.4f}".format(a))
Output:20.00
20.000
20.0000

From the above code, we can further deduce that the format() function can also be used to convert a specific number of decimal points.

round() function to print floats to a specific number of decimal points in Python

The method can be best explained with an example given below:

Example:

b = 6.456453 #Initialising the variable "b"

#using round() function to print the value till 3 decimal places
print(round(b,3))

#using round() function to print the value till 4 decimal places
print(round(b,4))

#using round() function to print the value till 5 decimal places
print(round(b,5))



Output:
6.456
6.4565
6.45645

From the above code, we can further conclude that the round() function can also be used to convert a specific number of decimal points.

Also read:

3 responses to “Print floats to a specific number of decimal points in Python”

  1. murali says:

    simpler examples …i like it!keep it coming to help us, thank you.

  2. James hezel says:

    is there any way to insert a variable instead of a number for the %.1f % x command?

Leave a Reply

Your email address will not be published. Required fields are marked *