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 floating-point numbers. It includes the integer part and decimal part. Sometimes, we need only a few decimal places in that floating-point number. So we will see how to get the float value with only two decimal places.
In python, we can create a floating-point 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.
Frequently Asked:
- Get position of a character in a string in Python
- Check if First Letter of String is Uppercase in Python
- Python: Get difference between two datetimes in milliseconds
- Convert a List to Set in Python
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 floating-point number
- n represents the total number of decimal places to be rounded in the given floating-point number.
Here, we have to round off the floating-point number to 2 decimal places. So, n=2
round(float_value, 2)
Example:
In this example, We will create a floating-point 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 floating-point number
- .nf represents a string format that will return floating-point numbers with n decimal places.
Here, n=2
"{:.2f} ".format(float_value)
Example:
In this example, We will create a floating-point 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 floating-point 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 floating-point 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 floating-point 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 floating-point number.
Example:
In this example, we will create a floating-point 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 floating-point 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 floating-point number.
Example:
In this example, We will create a floating-point 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.