How To Resolve AttributeError: ‘Float’ Object Has No Attribute ‘Round’ In Python

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

If you encounter the AttributeError: ‘float’ object has no attribute ’round’ and don’t know how to solve it, you can refer to this article. You can pass a float object as the round() function parameter and a float object on the numpy function. Read the following article.

What causes the AttributeError: ‘float’ object has no attribute ’round’?

The round() function is a built-in high-level function in Python. It has the effect of rounding a given number. The number is returned as a floating point, the number of digits after the specified comma. The digit after the comma defaults to 0 if left so that the function will return the nearest integer.

Syntax:

round(number[, ndigits])

Parameters:

  • number: Number you want to round.
  • ndigits: The number of digits you want after the decimal point.

Return value from the round() function:

  • If there are no ndigits, the function returns the nearest integer.
  • If two arguments are provided, the function returns a number with ndigits decimal places after the decimal point.

The AttributeError: ‘float’ object has no attribute ’round’ because you call the round() function on the float object.

Example

myFloat = 1.12

# Use the round() function
print(myFloat.round())

Output:

Traceback (most recent call last):
  File "code.py", line 4, in <module>
    print(myFloat.round())
AttributeError: 'float' object has no attribute 'round'

How to solve this error?

Float object as a round function parameter

As stated above, you need to set the float object as the round() function parameter then the error will be resolved.

Example:

  • Initialize a variable whose value is float.
  • The float object must be passed as a parameter to the round() function.
myFloat = 1.12

# Use the round() function
print('Value when rounding:', round(myFloat))

Output:

Value when rounding: 1

If you want to get the digit after the decimal point, let’s change the 2nd parameter.

Example:

myFloat = 1.12

# Use the round() function. Get one digit after the decimal point
print('Value when rounding:', round(myFloat,1))

Output:

Value when rounding: 1.1

The round() function in the Numpy module

You can use the round() function in the Numpy module to round elements in a Numpy array.

Syntax:

np.round(object, ndigits)

Parameters:

  • ndigits: The number of digits you want after the decimal point.
  • object: numpy array.

Returns an array with the elements in the array rounded to the specified number of decimals.

Or a function that has the same functionality as np.round:

np.around(object, ndigits)

Example:

  • Initialize the numpy module.
  • Create a numpy array.
  • Use the np.round() function and the np.around() function to round the elements in the numpy array.
import numpy as np

numpyArray = [1.12, 1.13, 1.23]

# Use the np.round() to round numpy array
print('The numpy array has been rounded by the round() function:', np.round(numpyArray))

# Use the np.around() to round numpy array
print('The numpy array has been rounded by the around() function:', np.around(numpyArray))

Output:

The numpy array has been rounded by the round() function: [1. 1. 1.]
The numpy array has been rounded by the around() function: [1. 1. 1.]

Summary

The AttributeError: ‘float’ object has no attribute ’round’ is relatively easy to solve. Solution 1: “set the float object as the round() function parameter” is better for this problem. Hope after reading the article you can solve this problem. If you have any more ideas on this, let us know. Thank you for taking the time to read the article.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *