Can we round the mean to a specified number of decimal places?

Question

In the context of this exercise, can we round the mean to a specified number of decimal places?

Answer

To round a single value to a number of decimal places, we can utilize the round() function in Python.

To use the round() function you can pass in a number to round, and an optional second value which is the number of decimal places. If you do not input a second value, then it will round to 0 decimal places by default.

Numpy provides a way to round every value in an array, through the np.round() function.

The np.round() function works similar to the round() function in Python, because you can input a specific number of decimal places to round each value in the array to.

Example

number = 1.234
print round(number, 2) # 1.23
print round(number) # 1.0

array = np.array([1.11, 2.22, 3.33, 4.44])
print np.round(array, 1)
# [1.1, 2.2, 3.3, 4.4]
5 Likes