Limit a float to two decimal places in Python

In this Python tutorial, you will learn how to limit a float to two decimal places.

Table Of Contents

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:

Advertisements
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.

  1. The float_value is the floating-point number
  2. 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.

  1. float_value is the floating-point number
  2. .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.

Pandas Tutorials -Learn Data Analysis with Python

   

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 sought-after 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.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top