Evenly round to the given number of decimals. Parameters ---------- a : array_like Input data. decimals : int, optional Number of decimal places to round to (default: 0). If decimals is negative, it specifies the number of positions to the left
(a, decimals=0, out=None)
| 3619 | |
| 3620 | @array_function_dispatch(_round_dispatcher) |
| 3621 | def round(a, decimals=0, out=None): |
| 3622 | """ |
| 3623 | Evenly round to the given number of decimals. |
| 3624 | |
| 3625 | Parameters |
| 3626 | ---------- |
| 3627 | a : array_like |
| 3628 | Input data. |
| 3629 | decimals : int, optional |
| 3630 | Number of decimal places to round to (default: 0). If |
| 3631 | decimals is negative, it specifies the number of positions to |
| 3632 | the left of the decimal point. |
| 3633 | out : ndarray, optional |
| 3634 | Alternative output array in which to place the result. It must have |
| 3635 | the same shape as the expected output, but the type of the output |
| 3636 | values will be cast if necessary. See :ref:`ufuncs-output-type` |
| 3637 | for more details. |
| 3638 | |
| 3639 | Returns |
| 3640 | ------- |
| 3641 | rounded_array : ndarray |
| 3642 | An array of the same type as `a`, containing the rounded values. |
| 3643 | Unless `out` was specified, a new array is created. A reference to |
| 3644 | the result is returned. |
| 3645 | |
| 3646 | The real and imaginary parts of complex numbers are rounded |
| 3647 | separately. The result of rounding a float is a float. |
| 3648 | |
| 3649 | See Also |
| 3650 | -------- |
| 3651 | ndarray.round : equivalent method |
| 3652 | around : an alias for this function |
| 3653 | ceil, fix, floor, rint, trunc |
| 3654 | |
| 3655 | |
| 3656 | Notes |
| 3657 | ----- |
| 3658 | For values exactly halfway between rounded decimal values, NumPy |
| 3659 | rounds to the nearest even value. Thus 1.5 and 2.5 round to 2.0, |
| 3660 | -0.5 and 0.5 round to 0.0, etc. |
| 3661 | |
| 3662 | ``np.round`` uses a fast but sometimes inexact algorithm to round |
| 3663 | floating-point datatypes. For positive `decimals` it is equivalent to |
| 3664 | ``np.true_divide(np.rint(a * 10**decimals), 10**decimals)``, which has |
| 3665 | error due to the inexact representation of decimal fractions in the IEEE |
| 3666 | floating point standard [1]_ and errors introduced when scaling by powers |
| 3667 | of ten. For instance, note the extra "1" in the following: |
| 3668 | |
| 3669 | >>> np.round(56294995342131.5, 3) |
| 3670 | 56294995342131.51 |
| 3671 | |
| 3672 | If your goal is to print such values with a fixed number of decimals, it is |
| 3673 | preferable to use numpy's float printing routines to limit the number of |
| 3674 | printed decimals: |
| 3675 | |
| 3676 | >>> np.format_float_positional(56294995342131.5, precision=3) |
| 3677 | '56294995342131.5' |
| 3678 |
searching dependent graphs…