NumPy Tutorial · NumPy ufunc

ufunc Rounding Decimals

Learn all about ufunc Rounding Decimals in this comprehensive tutorial.

5 min read advanced
  • There are primarily five ways of rounding off decimals in NumPy:
  • Remove the decimals, and return the float number closest to zero.
  • The around() function increments preceding digit or decimal by 1 if >=5 else do nothing.
  • The floor() function rounds off decimal to nearest lower integer.
  • The ceil() function rounds off decimal to nearest upper integer.

Rounding Decimals

There are primarily five ways of rounding off decimals in NumPy:

  • truncation
  • fix
  • rounding
  • floor
  • ceil

Truncation

Remove the decimals, and return the float number closest to zero. Use the trunc() and fix() functions.

python
python

Rounding

The around() function increments preceding digit or decimal by 1 if >=5 else do nothing.

E.g. round off to 1 decimal point, 3.16666 is 3.2

python

Floor

The floor() function rounds off decimal to nearest lower integer.

E.g. floor of 3.166 is 3.

python

Ceil

The ceil() function rounds off decimal to nearest upper integer.

E.g. ceil of 3.166 is 4.

python

Module quiz

2 questions
1

Which of the following is true about ufunc Rounding Decimals?

2

What is the most common pitfall when working with ufunc Rounding Decimals?

Answer all questions to submit.