|
| 1 | +--- |
| 2 | +Title: 'round()' |
| 3 | +Description: 'Returns a new array with each element rounded to the specified number of decimals.' |
| 4 | +Subjects: |
| 5 | + - 'Code Foundations' |
| 6 | + - 'Computer Science' |
| 7 | +Tags: |
| 8 | + - 'Arrays' |
| 9 | + - 'Elements' |
| 10 | + - 'Methods' |
| 11 | + - 'NumPy' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-python-3' |
| 14 | + - 'paths/data-science' |
| 15 | +--- |
| 16 | + |
| 17 | +The **`round()`** method in NumPy rounds each element of an [array](https://www.codecademy.com/resources/docs/numpy/ndarray) to the nearest integer or to a specified number of decimal places. Values exactly halfway between two numbers are rounded to the nearest even value (also known as banker’s rounding). |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +ndarray.round(decimals=0, out=None) |
| 23 | +``` |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +- `decimals` (int, optional): Number of decimal places to round to. Default is 0. Can be negative to round to the left of the decimal point. |
| 28 | +- `out` (ndarray, optional): An alternative output array where results are stored. Must be the same shape as the input. |
| 29 | + |
| 30 | +**Return value:** |
| 31 | + |
| 32 | +Returns an array with elements rounded to the specified number of decimals. If `out` is provided, the result is stored in that array, and a reference to it is returned. |
| 33 | + |
| 34 | +## Example 1: Round to the nearest whole number |
| 35 | + |
| 36 | +This example rounds all elements in a NumPy array to the nearest integer: |
| 37 | + |
| 38 | +```py |
| 39 | +import numpy as np |
| 40 | + |
| 41 | +arr = np.array([1.34566, 4.55, 7.788, 2.09, 9.45]) |
| 42 | +rounded_arr = arr.round() |
| 43 | + |
| 44 | +print(rounded_arr) |
| 45 | +``` |
| 46 | + |
| 47 | +This produces the following output: |
| 48 | + |
| 49 | +```shell |
| 50 | +[1. 5. 8. 2. 9.] |
| 51 | +``` |
| 52 | + |
| 53 | +## Example 2: Round to specific decimal places |
| 54 | + |
| 55 | +This example rounds all elements in a NumPy array to two decimal places: |
| 56 | + |
| 57 | +```py |
| 58 | +import numpy as np |
| 59 | + |
| 60 | +ndarray = np.array([1.34566, 4.55, 7.788, 2.09, 9.45]) |
| 61 | +two_decimal_places = np.round(ndarray, decimals=2) |
| 62 | + |
| 63 | +print(two_decimal_places) |
| 64 | +``` |
| 65 | + |
| 66 | +This produces the following output: |
| 67 | + |
| 68 | +```shell |
| 69 | +[1.35 4.55 7.79 2.09 9.45] |
| 70 | +``` |
| 71 | + |
| 72 | +## Codebyte Example |
| 73 | + |
| 74 | +This example shows how to round NumPy array elements to various decimal places using `round()`: |
| 75 | + |
| 76 | +```codebyte/python |
| 77 | +import numpy as np |
| 78 | +
|
| 79 | +ndarray = np.array([1.34566, 4.55, 7.788, 2.09, 9.45]) |
| 80 | +
|
| 81 | +# Round off to the nearest whole number |
| 82 | +nearest_whole_number = np.round(ndarray) |
| 83 | +print(nearest_whole_number) |
| 84 | +
|
| 85 | +# Round off to 2 decimal places |
| 86 | +two_decimal_places = np.round(ndarray, decimals=2) |
| 87 | +print(two_decimal_places) |
| 88 | +``` |
0 commit comments