Rounding Decimals In NumPy Ufunc

In NumPy, there are several functions for rounding decimals, each with its own objective and execution.

The purpose of this article is to explore the different ways in which we can round decimals when utilizing NumPy.



Rounding Decimals

Numpy ufuncs include several functions that can be used to round decimals in arrays.

There are mainly five most used functions to rounding decimals:

FunctionsOverview
TruncationTruncation is the process of removing the decimal part of a number and returning the integer part.

Positive numbers are truncated towards zero while negative numbers are truncated towards negative infinity.

FixFix is a function that rounds a number towards zero, returning the nearest integer towards zero.

If the number is positive, it rounds down to the nearest integer. If the number is negative, it rounds up to the nearest integer.

RoundingRounding is the process of approximating a number to a specified number of decimal places.

There are several methods of rounding, including rounding up, rounding down, rounding to the nearest even number, and rounding to the nearest odd number.

FloorFloor is a function that rounds a number down to the nearest integer or decimal.

The result is always less than or equal to the original number.

The floor function is useful for converting continuous variables into categorical variables.

CeilCeil is a function that rounds a number up to the nearest integer or decimal.

The result is always greater than or equal to the original number.

The ceil function is useful for converting continuous variables into categorical variables, similar to the floor function.

 


Truncation

Truncation is a quick and simple way to remove the decimal part of a number and obtain the integer part.

It can be useful when working with financial data, where it is often necessary to work with whole numbers only.

You can utilize the functions trunc() and fix() to perform the operation.

In the following mrx_arr array, truncate the items as follows:

Example: 

import numpy as npy mrx_arr = npy.trunc([7.2588, -5.8777, 7.9111]) print(mrx_arr)

Truncate the element first in the below array then apply the absolute() function:

Example: 

import numpy as npy mrx_arr = npy.trunc([-56.1384, -89.9122]) mrx_arr = npy.absolute(mrx_arr) print(mrx_arr)

Fix

Fix is useful when you need to round a number towards zero.

It can be helpful when dealing with data that needs to be rounded down or up depending on the sign of the number.

Here is the identical example as above but utilizing the fix() function:

Example: 

import numpy as npy mrx_arr = npy.fix([7.2588, -5.8777, 7.9111]) print(mrx_arr)

Implement the absolute() function after the fix() function:

Example: 

import numpy as npy mrx_arr = npy.fix([-56.1384, -89.9122]) mrx_arr = npy.absolute(mrx_arr) print(mrx_arr)

Rounding

According to rounding decimals, the around() function increases the preceding digit by 1 if the value is higher than 5, otherwise, it does nothing.

The number 7.2545 can be rounded off to 2 decimal points, and it is going to be 7.25

Calculate 7.0 by rounding off 7.2545:

Example: 

import numpy as npy mrx_arr = npy.around(7.2545) print(mrx_arr)

Rounding can help you obtain a more simplified representation of a number.

It can be useful when working with measurements or data that needs to be reported in a specific format or precision.

In the following example, round off -26.5644 to 2 decimal positions:

Example: 

import numpy as npy mrx_arr = npy.around(-26.5644, 2) print(mrx_arr)

Floor

In the floor() function, decimal numbers are rounded to the next smaller integer.

For example, the floor of -6.2333 is -7.

In the following array, floor number 5.923 is as follows:

Example: 

import numpy as npy mrx_arr = npy.floor([5.923]) print(mrx_arr)

Floor is helpful when you need to round a number down to the nearest integer or decimal.

It can be useful when dealing with categorical data, as you can use floor to convert continuous variables into categories.

Floor the following numbers in the mrx_arr array:

Example: 

import numpy as npy mrx_arr = npy.floor([19.6564, -7.2422, -16.8999]) print(mrx_arr)
Note: A float is generated by the floor() function, whereas an integer is generated by trunc().

Ceil

A decimal value is rounded to the closest higher integer by calling the ceil() function.

For example, the ceiling of 6.2111 is 7.

In the following array, ceil number 6.2111 is as follows:

Example: 

import numpy as npy mrx_arr = npy.ceil([6.2111]) print(mrx_arr)

Ceil can be useful when you need to round a number up to the nearest integer or decimal.

It is also helpful for converting continuous variables into categories, similar to floor.

Ceil the values of the following mrx_arr array then execute the absolute() function:

Example: 

import numpy as npy mrx_arr = npy.absolute(npy.ceil([-3.94, -25.45, -0.67])) print(mrx_arr)

 

Example Explanation

Above example creates a list of three negative numbers, [-3.94, -25.45, -0.67], and applies two NumPy functions to it: npy.ceil() and npy.absolute().

npy.ceil() takes the ceiling of each number in the list, which means it rounds up to the nearest integer. So the resulting list is [-3, -25, -0].

npy.absolute() takes the absolute value of each number in the resulting list.

The absolute value is the distance of a number from zero, so it makes all the values positive. So the final output of the code is shown in below Image.

Rounding Decimals in Numpy

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *