Select Page

How to Solve Python AttributeError: ‘float’ object has no attribute ’round’

by | Programming, Python, Tips

The built-in Python function round() returns a rounded version of a specified floating-point number to a specified number of decimal places. The round() function does not belong to the float data type. If you try to call the round() method on a floating-point number, you will raise the AttributeError: ‘float’ object has no attribute ’round’.

The correct way to use the round() function is to pass the floating-point number and the number of decimal places to the function as parameters. For example rounded_number = float(number, 2).

This tutorial will go through how to solve this error with the help of code examples.


AttributeError: ‘float’ object has no attribute ’round’

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The part “‘float’ object has no attribute ‘round’” tells us that the float data type does not have the attribute round(). The round() function is built-in to Python, which means they are always available. The round() function returns a rounded version of a specified floating-point number to a specified number of decimal places.

The syntax for the round() function is:

round(number, digits)

Parameters

  • number: Required. The number to round
  • digits: Optional. The number of decimal places to round. Default is 0.

Let’s see what happens when we try to round a floating-point number to two decimal places using the built-in round() function:

pi = 3.14285

rounded_pi = round(pi, 2)

print(f'pi rounded to two decimal places is: {rounded_pi}')
pi rounded to two decimal places is: 3.14

We successfully round the number to two decimal places.

Let’s see what happens when we try to call the round function on a floating-point number:

pi = 3.14285

rounded_pi = pi.round(2)

print(f'pi rounded to two decimal places is: {rounded_pi}')
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-facd1083ac99> in <module>
      1 pi = 3.14285
      2 
----> 3 rounded_pi = pi.round(2)
      4 
      5 print(f'pi rounded to two decimal places is: {rounded_pi}')

AttributeError: 'float' object has no attribute 'round'

The error occurs because the round() function does not belong to the float data type.

Example

Let’s look at an example where we have a list of numbers that we want to square and sum up. We want to round the sum to 1 decimal place. The code is as follows:

lst = [1.2, 3.4, 5.6, 6.7, 8.9, 10.1]

lst_sum_squared = sum([x ** 2 for x in lst]).round(1)

print(f'The sum of the squares of the array to one decimal place is {lst_sum_squared}')

In the above code, we use list comprehension to get the square of each element in the list. Then we use the built-in sum function, which will return a floating-point number. Then we call the round function and pass the number of decimal places as a parameter to it. Let’s run the code to see what happens:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-9d190ebd4012> in <module>
      1 lst = [1.2, 3.4, 5.6, 6.7, 8.9, 10.1]
----> 2 lst_sum_squared = sum([x ** 2 for x in lst]).round(1)
      3 print(f'The sum of the squares of the array to one decimal place is {lst_sum_squared}')

AttributeError: 'float' object has no attribute 'round'

The error occurs because we are incorrectly using the round() function. We cannot call the round() function on a floating-point number because the function does not belong to the float data type.

Solution

We need to use the correct syntax for the round() function to solve this error. We need to pass the number we want to round as the first parameter, which is sum([x ** 2 for x in lst] and the number of decimal places to round as the second parameter, which is 1. Let’s look at the revised code:

lst = [1.2, 3.4, 5.6, 6.7, 8.9, 10.1]

lst_sum_squared = round(sum([x ** 2 for x in lst]), 1)

print(f'The sum of the squares of the array to one decimal place is {lst_sum_squared}')

Let’s run the code to see the result:

The sum of the squares of the array to one decimal place is 270.5

We successfully round the sum of the squares of the list to one decimal place.

Summary

Congratulations on reading to the end of this tutorial! The AttributeError ‘float’ object has no attribute ’round’ occurs when you incorrectly call the round() function on a floating-point number. The correct syntax of round() is to pass the number you want to round and the number of decimal places to round as parameters.

For further reading on AttributeErrors, go to the articles:

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!