Python number of decimal places

This tutorial will demonstrate how to perform rounding off of a float value in Python to the nearest two decimal places.

Using round() Function

The rounded form of the requested value is provided by the round() function, which returns a floating point value with the given number of decimals.

Since the default value is 0 decimals, the method will give the closest integer if the number of decimal places is not specified.

Syntax

Parameters:-

  • number (required)- The number to be rounded
  • digits (optional)- Up to how many decimals the number needs to be rounded. The default value is 0.

To store the given number, we will define a variable. The round() method will round the given float number up to two decimal places by passing the given number and the number of decimal places (2 in this case) to it as arguments. We will print the rounded result of the given floating-point figure up to two decimals.

Example

The round() method is used in the program that follows to give the rounded result of the supplied floating-point value up to two decimals:

Code

Output:

Rounding 3.3469456 up to two decimal places: 3.35

Using format() Function

We'll create a variable to hold the given number. By giving the original number and format (up to the 2 decimal places) as arguments to the format() method, one can round a value up to two decimal places. It returns the given number in the format that the format specifier has defined.

Example

The following program will use the format() method to give the rounded number of the given floating-point value up to two decimals:

Code

Output:

Rounding 3.3469456 upto 2 decimal places: 3.35

Using Decimal Module

The decimal module of Python contributes to improving the precision of floating-point values. We must import the Python decimal module before we can utilize it.

floatnumber.Decimal(decimal) provides a 50-digit decimal point by default.

Here, we can round up to two digits decimal place by using value.quantize(decimal.Decimal('0.00')).

We will import the Python decimal module using the keyword import. We will create a variable to store the original number. We will use the Decimal() method of the decimal module to convert the provided floating-point value. The number has to be rounded up to two decimal places. Therefore, we use the value.quantize(decimal.Decimal()) function to give only two digits (2 zeros in the argument) after the decimal point. We'll obtain the results we want.

Example

The following program uses the decimal module to give the rounded-off value of the supplied floating-point value up to two decimals.

Code

Output:

Rounding 35.67533 upto 2 decimal places:  35.68

Using ceil() Function

A given number's ceiling value, the smallest integer number larger than or equal to that number, is returned by the ceil() function.

To import the math module, use the Python import keyword. To store the supplied floating-point value, we'll make a variable. To round the integer to two decimal digits and display the result, use the ceil() function.

Example

The ceil() method is used in the program below to return the rounded-off value of the supplied floating-point value up to two decimals.

Code

Output:

Rounding 4.83622 upto 2 decimal places: 
4.84

In this tutorial, we learned four alternative ways to round a given floating-point value in Python upto two decimal digits. We discovered how to round numbers to two decimal points using the ceil method and some mathematical reasoning. We also learned how to quantize a floating-point number by converting it to a decimal using Python's decimal module.

In Python, we can represent decimal values using the float datatype. The decimal point divides the integer from the fractional part. The maximum value a float object can have is 1.8 x 10^308.

In this article, we will discuss how to count the decimal places in Python. We will take a float value and return the total number of digits after the decimal point.

Using the decimal library to count decimal places in Python

The decimal library allows us to work with float values. We can convert these values to objects of the Decimal class from this library. There are many methods associated with this class that can perform various functions on these values.

The as_tuple() function is used to return the information about the floating value in a tuple. It returns the sign of the number (positive or negative), the digits in the number, and the exponent value.

The exponent attribute can be used to return the exponent value from this tuple. This exponent value is the same as the number of digits in the decimal place with a negative sign. We can use the abs() function to get the absolute value of this attribute, removing the negative sign.

See the code below.

Using decimal library

1

2

3

4

5

 

import decimal

d = decimal.Decimal('7.856')

print(abs(d.as_tuple().exponent))    

 

Output:

3

In the above example, we use the decimal.Decimal() constructor to create an object of the Decimal class. We get the exponent value of this object using the as_tuple() function. Its absolute value is displayed using the abs() function.

Using the float3 and float4 function to count decimal places in Python

The float3 and float4 function work with string objects. The former is used to return the length of the given string and the latter can split a given string into a list of substrings based on some character.

We can use these functions together to count decimal places in Python.

First, we will convert the float value to a string using the float7 function. Then we will split the list based on the float8 character to return two substrings. The second substring contains the decimal part and its length can be calculated using the float3 function.

For example,

Using len and split functions

1

2

3

4

5

 

a = 5.758

n = len(str(a).split(".")[1])

print(n)    

 

Output:

3

Further reading:

How to Format Float to 2 Decimal Places in Python

Read more →

Print Percentage Sign in Python

Read more →

Using the decimal0 function to count decimal places in Python

The decimal0 function is used to return the index of the first occurrence of an element in a string. We can use it with string slicing to count decimal places in Python.

We can slice string using the square brackets (decimal2). In these brackets, we can specify the start, end, and step values for the string slicing. We can specify the step as decimal3 to reverse the string. We can convert a float value to a string using the float7 function and reverse it using this method.

After reversing the string, the decimal part comes in front. We can find the occurrence of the decimal point using the decimal0 function. The index of the decimal point will be the same as the total decimal places.

This logic is implemented in the code below.

Using the find() function

1

2

3

4

5

 

a = 7.856

n = str(a)[::-1].find('.')

print(n)

 

Output:

3

Conclusion

To conclude, we discussed several methods to count decimal places in Python. In our first method, we used the Decimal class from the decimal library. We can use the as_tuple() function to get the exponent value from the objects of this class that represent the decimal places.

The other two methods involved converting the float value to a string. In the first method, we split the string into the real and fractional parts using the float4 function and found the decimal places using the float3 function on the fractional part.

In the final method, we reversed the number using string slicing and found the occurrence of the decimal point which corresponds to the total decimal places.

How do you find the number of decimal places in Python?

Using the find() function to count decimal places in Python The find() function is used to return the index of the first occurrence of an element in a string. We can use it with string slicing to count decimal places in Python.

How do you only get 3 decimal places in Python?

This is basically the correct answer, just use val = '%. 3f'%(1324343032.324325235) instead of print . ... .
This answer is correct, if you want to round (up) to a given number of decimal places. ... .
'%.2f' % 8866.316 is round but not truncate. ... .
you are rounding; take a look at my answer. ... .
This it not truncating..

How do I use .2f in Python?

2f is a placeholder for floating point number. So %d is replaced by the first value of the tuple i.e 12 and %. 2f is replaced by second value i.e 150.87612 . ... Python String Formatting..

How do you print to 5 decimal places in Python?

round() function to print floats to a specific number of decimal points in Python.