Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

numpy
python

What is the round_ function in NumPy?

Rukhshan Haroon

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The round_ function is used to round an array of floats to a given number of decimals. It comes with NumPy, which is a widely used library of the high-level programming language Python.

To use the round_ function, the round_ library must be imported at the start of the program.

import numpy

Syntax

numpy.round_(a, decimals=0, out=None)

Paramaters

The round_ function takes in the following parameters:

  • a - an array of floats on which the round_ function will be applied.
  • decimals - (optional) the number of decimals to which the values in a will be rounded. By default, its value is zero.
  • out - (optional) the location where the return value of the round_ function will be stored.

Return value

The round_ function returns an array with the same dimension as that of a and contains rounded values of type float.

If a contains complex numbers, the real and imaginary parts are rounded separately.

Example

The following example demonstrates how to round an array of floats up to three, and subsequently four, decimal numbers.

The return value is printed using the built-in print function, which comes with Python.

import numpy
print(numpy.round_([2.51231, 6.12351, -6123.123123], 3))
print(numpy.round_([2.51231, 6.12351, -6123.123123], 4))

RELATED TAGS

numpy
python

CONTRIBUTOR

Rukhshan Haroon
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring