# How to Specify the Number of Decimal Places in Python

09/11/2021

Contents

In this article, you will learn how to specify the number of decimal places in Python.

## Specify the Number of Decimal Places

### Using the round() function

To specify the number of decimal places in Python, you can use the round() function. The round() function takes two arguments: the first argument is the number you want to round, and the second argument is the number of decimal places to which you want to round. Here is an example:

``````x = 3.14159265359
y = round(x, 2)  # round x to 2 decimal places
print(y)  # Output: 3.14
``````

In this example, we have assigned the value 3.14159265359 to the variable x. We then use the round() function to round x to 2 decimal places and assign the result to the variable y. Finally, we print the value of y, which is 3.14.

### Using string formatting syntax

If you want to format the output as a string with a specific number of decimal places, you can use Python’s string formatting syntax. Here is an example:

``````x = 3.14159265359
y = 2
formatted_string = f"{x:.{y}f}"
print(formatted_string)  # Output: 3.14
``````

In this example, we have assigned the value 3.14159265359 to the variable x, and the value 2 to the variable y. We then use f-strings to format x as a string with y decimal places. The expression f”{x:.{y}f}” evaluates to the string “3.14”, which we then print to the console.

### Using the % operator

You can use the % operator to format a string with a specific number of decimal places. Here’s an example:

``````x = 3.14159265359
y = 2
formatted_string = "%.{}f".format(y) % x
print(formatted_string)  # Output: 3.14
``````

In this example, we use the % operator to format a string with a placeholder for a float value, followed by the number of decimal places we want to display. We then use the format() method to replace the placeholder with the value of y. Finally, we use the % operator again to substitute the value of x into the formatted string.

### Using the format() Method

You can use the format() method to format a string with a specific number of decimal places. Here’s an example:

``````x = 3.14159265359
y = 2
formatted_string = "{:.{}f}".format(x, y)
print(formatted_string)  # Output: 3.14
``````

In this example, we use the format() method to format a string with a placeholder for a float value, followed by the number of decimal places we want to display. We then use the method’s positional arguments to substitute the values of x and y into the formatted string.

### Using f-Strings

You can use f-strings to format a string with a specific number of decimal places. Here’s an example:

``````x = 3.14159265359
y = 2
formatted_string = f"{x:.{y}f}"
print(formatted_string)  # Output: 3.14
``````

In this example, we use an f-string to format a string with a placeholder for a float value, followed by the number of decimal places we want to display. We then use the f-string’s expression to substitute the values of x and y into the formatted string.

### Using the Decimal module

You can use Python’s Decimal module to control the precision of floating-point numbers. Here’s an example:

``````from decimal import Decimal

x = 3.14159265359
y = 2

result = Decimal(x).quantize(Decimal('.' + '0'*y))
print(result)  # Output: 3.14
``````

In this example, we first import the Decimal class from the decimal module. We then create a Decimal object with the value of x, and use the quantize() method to specify the precision of the number. We pass a Decimal object to the quantize() method that has a value of 0. followed by y zeros. This creates a decimal object with y decimal places, and then the quantize() method rounds the original number to that precision.

### Using the NumPy module

You can use the NumPy module to control the precision of floating-point numbers. Here’s an example:

``````import numpy as np

x = 3.14159265359
y = 2

result = np.around(x, decimals=y)
print(result)  # Output: 3.14
``````

In this example, we first import the NumPy module and use the np.around() function to round x to y decimal places. The np.around() function takes two arguments: the first argument is the number you want to round, and the second argument is the number of decimal places to which you want to round.

### Using the math module

You can use the math module to control the precision of floating-point numbers. Here’s an example:

``````import math

x = 3.14159265359
y = 2

result = math.floor(x * 10**y) / 10**y
print(result)  # Output: 3.14
``````

In this example, we first import the math module and use the math.floor() function to round x down to y decimal places. We then divide the result by 10**y to shift the decimal point back to its original position.