In this Python tutorial, you will learn how to limit a float to two decimal places.
Table Of Contents
 Introduction
 Limit a float to N decimal places using round() function
 Limit a float to N decimal places using str.format() with round()
 Limit a float to N decimal places using str.format() with round()
 Limit a float to N decimal places using String Formatting Operator
 Limit a float to N decimal places using String Formatting Operator with round()
 Summary
Let’s dive into the tutorial.
Introduction
Python supports floatingpoint numbers. It includes the integer part and decimal part. Sometimes, we need only a few decimal places in that floatingpoint number. So we will see how to get the float value with only two decimal places.
In python, we can create a floatingpoint number like the below:
float_value = 67.89433
Now we will learn how to limit a float to two decimal places like,
67.82
There are different ways to do this. Let’s discuss them one by one.
Limit a float to N decimal places using round() function
The round() function in python rounds the float value up to n decimal places.
Syntax:
round(float_value, n)
It takes two parameters.
 The float_value is the floatingpoint number
 n represents the total number of decimal places to be rounded in the given floatingpoint number.
Here, we have to round off the floatingpoint number to 2 decimal places. So, n=2
round(float_value, 2)
Example:
In this example, We will create a floatingpoint number and limit it to two decimal places.
# Create a float value float_value = 67.822342 # display actual float value print("Actual value: ",float_value) # formatted value print("2 Decimal places: ",round(float_value, 2))
Output:
Actual value: 67.822342 2 Decimal places: 67.82
We can see that 67.822342 is returned as 67.82 by limiting only two decimal values.
Limit a float to N decimal places using str.format() with round()
The str.format() is used to format the string. It used to format the given floatting point number to return with 2 decimal places using string formatter – {:.2f}.
Syntax:
"{:.nf} ".format(float_value)
It takes two parameters.
 float_value is the floatingpoint number
 .nf represents a string format that will return floatingpoint numbers with n decimal places.
Here, n=2
"{:.2f} ".format(float_value)
Example:
In this example, We will create a floatingpoint number and limit it to two decimal places.
# create a float value float_value = 67.822342 # display actual float value print("Actual value: ",float_value) # rounded value print("2 Decimal places: {:.2f} ".format(float_value))
Output:
Actual value: 67.822342 2 Decimal places: 67.82
We can see that 67.822342 is returned as 67.82 by limiting only two decimal values.
Limit a float to N decimal places using str.format() with round()
It can be possible to use the round() function with str.format() function. It will limit a floatingpoint number to two decimal places. The round() is placed within the format() parameter.
Syntax:
"{:.2f} ".format(round(float_value, 2))
Example:
In this example, We will create a floatingpoint number and limit it to two decimal places using str. format() with the round() function.
# create a float value float_value = 67.822342 # display actual float value print("Actual value: ",float_value) # rounded value with str.format() print("2 Decimal places: {:.2f} ".format(round(float_value,2)))
Output:
Actual value: 67.822342 2 Decimal places: 67.82
We can see that 67.822342 is returned as 67.82 by limiting only two decimal values.
Limit a float to N decimal places using String Formatting Operator
In python, we can use % as a string formatting operator. It will represent a string. If we want to represent floatingpoint values using this operator, then we have to specify – “%.nf”, where n represents the number of decimal places to be returned. Hence we can limit a float to two decimal places using this string formatting operator.
Syntax:
"%.nf" % float_value
Here, n =2
"%.2f" % float_value
The float_value is the input floatingpoint number.
Example:
In this example, we will create a floatingpoint number and limit it to two decimal places using a string formatting operator.
# create a float value float_value = 67.822342 # display actual float value print("Actual value: ",float_value) # rounded value with string formatting operator print("2 Decimal places: %.2f" % float_value)
Output:
Actual value: 67.822342 2 Decimal places: 67.82
We can see that 67.822342 is returned as 67.82 by limiting only two decimal values using String Formatting Operator.
Limit a float to N decimal places using String Formatting Operator with round()
In python, we can use % as a string formatting operator. It will represent a string. If we want to represent floatingpoint values using this operator along with the round() function, then we have to specify – “%.nf”, where n represents the number of decimal places to be returned. After %, we can apply round() function. Hence we can limit a float to two decimal places using this string formatting operator.
Syntax:
"%.2f" % round(float_value, 2)
The float_value is the input floatingpoint number.
Example:
In this example, We will create a floatingpoint number and limit it to two decimal places using a string formatting operator with round().
# create a float value float_value = 67.822342 # display actual float value print("Actual value: ",float_value) # rounded value with string formatting operator print("2 Decimal places: %.2f" % round(float_value, 2))
Output:
Actual value: 67.822342 2 Decimal places: 67.82
We can see that 67.822342 is returned as 67.82 by limiting only two decimal values using String Formatting Operator with round() function.
Summary
From this tutorial, we learned how to limit a float value to two decimal places using str.format(), string formatting operator, and round() functions. It can be possible to use the round() function with str.format() and string formatting operator. If you want to limit a float to n decimal places, then you need to keep the n value based on the decimal places you need to return. Happy Learning.
Pandas Tutorials Learn Data Analysis with Python

Pandas Tutorial Part #1  Introduction to Data Analysis with Python

Pandas Tutorial Part #2  Basics of Pandas Series

Pandas Tutorial Part #3  Get & Set Series values

Pandas Tutorial Part #4  Attributes & methods of Pandas Series

Pandas Tutorial Part #5  Add or Remove Pandas Series elements

Pandas Tutorial Part #6  Introduction to DataFrame

Pandas Tutorial Part #7  DataFrame.loc[]  Select Rows / Columns by Indexing

Pandas Tutorial Part #8  DataFrame.iloc[]  Select Rows / Columns by Label Names

Pandas Tutorial Part #9  Filter DataFrame Rows

Pandas Tutorial Part #10  Add/Remove DataFrame Rows & Columns

Pandas Tutorial Part #11  DataFrame attributes & methods

Pandas Tutorial Part #12  Handling Missing Data or NaN values

Pandas Tutorial Part #13  Iterate over Rows & Columns of DataFrame

Pandas Tutorial Part #14  Sorting DataFrame by Rows or Columns

Pandas Tutorial Part #15  Merging or Concatenating DataFrames

Pandas Tutorial Part #16  DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most soughtafter professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.