Menu Close

How to Limit Decimal Places in Python

How to limit decimal places in Python

Hi Python Developers, Throughout this article you will see everything about how to limit decimal places in Python by using some Python built-in functions and modules with the help of proper examples and explanations.

Sometimes in real-life Python applications, we want to perform some calculations to two decimal places, In that scenario, we can use some functions and modules to limit decimal places in Python.

Why do we need to limit decimal places in Python?

Suppose we want to work on a real-life Python application and want to perform some mathematical operations, In that scenario, It might be your calculation produces a long decimal number but you want to only two or three decimal places, At that time you have to use some functions and packages to round float to 2 decimal places or format decimal to 3 decimal places.

Built-in Functions to Limit Decimal Places in Python

I have listed some Python built-in functions that are used to limit the decimal places in Python.

  • round(number, digits):- The round() function is a built-in function in Python that is used to return the rounded version of the passed number with a specified number of digits. It takes two parameter numbers to be rounded and a digit that defines a number of decimal places.
  • format(value, format):- It is also a Python built-in function that is used to limit decimal places in Python. It takes two arguments the value and format which you want to use for formatting the passed value.

Limit Decimal Places in Python using round() and format()

let’s write some Python programs to round the float to 2 decimal places using the round() and format() functions.

Example: Round float to 2 decimal places Python

# decimal value
decimal_value = 120.7572

# using round()
result = round(decimal_value, 2)

# displaying
print(result)

The Output will be:- 120.76

In the above example, the round() function returns the rounded version of the passed value.

Example:- Round float to 2 decimal places Python

# decimal value
decimal_value = 45.7572

# using round()
result = format(decimal_value, '.2f')

# displaying
print(result)

The output will be:- 45.76

Let’s see some other ways in order to round float to 2 decimal places in Python.

# decimal value
decimal_value = 45.7572

# using '%' to format the decimal number till 2 decimal places
print("Using '%' to format the decimal number till 2 decimal places:- ", end=' ')
print('%.2f' % decimal_value)


# using format() to format the decimal number to 3 decimal places
print("Using format() method to format the decimal number to 3 decimal places:- ", end=' ')
print('{0:.3f}'.format(decimal_value))


# using f-string to limit the decimal number to 2 decimal places
print("Using f-string method to format the decimal number to 3 decimal places:- ", end=' ')
print(f"{decimal_value:0,.2f}")

Output

Using '%' to format the decimal number till 2 decimal places:-  45.76
Using format() method to format the decimal number to 3 decimal places:-  45.757
Using f-string method to format the decimal number to 3 decimal places:-  45.76

In the above example, I have used percent sign ( % ), string format() functions, and f-string in order to set the decimal value to 2 decimal places, As you can see below output.

As you can see in the above examples, the round() and float() functions are returning the rounded version of the passed value.

How to Limit Decimal Places in Python Using Modules

Python provides some built-in and external packages in order to format the decimal value up to 2 decimal places. Let’s see some of the popular packages along with examples.

Using Python Numpy:

Python numpy is an external most popular and widely used Python package that provides numerous functions and methods in order to work with complex mathematical calculations. It has a method called round() and this method is used to round the passed decimal value.

import numpy as np

num = 3.4302789
rounded_num = np.round(num, 2)
print(rounded_num)

The output will be:- 3.43

Note:- Make sure, Numpy has been installed on your machine, If not then follow the below command to install the Python numpy library.

pip install numpy

Using Python Pandas:

Python Pandas is an external library written in Python programming, it is mostly used for data manipulation and data analysis. To use Python pandas, it must be installed on your machine.

Note:- Make sure, the Python pandas library has been installed on your machine, If not then follow the below command to install the Python pandas library.

pip install pandas
import pandas as pd

df = pd.DataFrame({'num': [56.6968798]})
df['rounded_num'] = df['num'].round(3)
print(df['rounded_num'][0])

The output will be:- 56.697

Using Python decimal Module:

The decimal is a built-in module in Python language that provides various methods and functions to work with decimal values. It is a built-in module, Therefore you don’t need to install it like numpy and Pandas.

from decimal import Decimal

num = Decimal('3.14159265359')
rounded_num = num.quantize(Decimal('.01'))
print(rounded_num)

In the above example, I have imported the Decimal class from the decimal module and created an object of the Decimal class called num by passing the decimal value 3.14159265359 as a string.
We then used quantize() method of the num object to format the passed decimal values up to 2 decimal places. Remember, It always returns the rounded value of passed decimal values.

Using Python math Module:

Again, the Python math module is a built-in module, Therefore it does not mandatory to install it by using the pip command. Just import it and start using it.

import math
num = 3.75657
rounded_num = math.floor(num * 100) / 100
print(rounded_num)

The output 3.75

In the above example, I imported the math module using the import keyword and used the floor() function of the math module to round down the value of the passed decimal number.

So, This is how you can use packages and modules to round the decimal number to 2 decimal places.

How to limit decimal places in Python without rounding

Sometimes we want to get exact decimal numbers up to 2 decimal places without doing any rounding. To do that, you can follow the below example.

import math
num = 3.76657
rounded_num = math.floor(num * 100) / 100
print(rounded_num)

Conclusion

In conclusion, We have seen multiple ways to limit decimal places in Python with rounding and without rounding the decimal places. You can use any one of them as per your requirement.
We have seen built-in functions like round() and format() and external packages like Pandas, Numpy, Math, and Decimal along with examples.

If you found this article helpful, Please share and keep visiting for further Python tutorials.

Python String Tutorial
Python Shallow and Deep Copy

Related Posts