
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
NumPy - Rounding Functions
NumPy Rounding Functions
Rounding functions in NumPy are used to round off the values in arrays to a specified number of decimal places. These functions are helpful in various scenarios, such as when you need to present values in a cleaner format or when performing numerical computations where precision control is necessary.
NumPy provides several rounding functions, including round(), floor(), ceil(), and trunc(), each serving a different purpose when it comes to rounding values.
Rounding Using round() Function
The numpy.round() function rounds each element in the input array to the specified number of decimal places. If no number of decimals is provided, it rounds to the nearest integer.
round(x, decimals) = rounded value to 'decimals' number of places
Example: Rounding Values
In the following example, we round an array of values to 2 decimal places using NumPy's round() function −
import numpy as np # Define an array of values values = np.array([3.14159, 2.71828, 1.61803, 0.57721]) # Round each element to 2 decimal places rounded_values = np.round(values, 2) print("Rounded values:", rounded_values)
The result of the above code is −
Rounded values: [3.14 2.72 1.62 0.58]
Flooring Using floor() Function
The numpy.floor() function rounds each element in the input array down to the nearest integer less than or equal to the element. This function always rounds down, regardless of the decimal part.
floor(x) = largest integer less than or equal to x
Example: Flooring Values
In the following example, we round down an array of values using NumPy's floor() function −
import numpy as np # Define an array of values values = np.array([3.14159, 2.71828, 1.61803, 0.57721]) # Round down each element to the nearest integer floored_values = np.floor(values) print("Floored values:", floored_values)
Following is the output obtained −
Floored values: [3. 2. 1. 0.]
Ceiling Using ceil() Function
The numpy.ceil() function rounds each element in the input array up to the nearest integer greater than or equal to the element. This function always rounds up, regardless of the decimal part.
ceil(x) = smallest integer greater than or equal to x
Example: Ceiling Values
In the following example, we round up an array of values using NumPy's ceil() function −
import numpy as np # Define an array of values values = np.array([3.14159, 2.71828, 1.61803, 0.57721]) # Round up each element to the nearest integer ceiled_values = np.ceil(values) print("Ceiled values:", ceiled_values)
This will produce the following result −
Ceiled values: [4. 3. 2. 1.]
Truncating Using trunc() Function
The numpy.trunc() function truncates each element in the input array by removing the decimal part. It essentially rounds the value towards zero, keeping the integer part.
trunc(x) = integer part of x, removing the decimal part
Example: Truncating Values
In the following example, we truncate an array of values using NumPy's trunc() function −
import numpy as np # Define an array of values values = np.array([3.14159, 2.71828, 1.61803, 0.57721]) # Truncate each element truncated_values = np.trunc(values) print("Truncated values:", truncated_values)
Following is the output of the above code −
Truncated values: [3. 2. 1. 0.]
Rounding to a Specific Multiple
The numpy.around() function is used to round elements in an array to the nearest specified multiple. This is different from the standard rounding because it allows for a custom rounding base.
around(x, decimals, out) = rounds to the nearest multiple of decimals
Example: Rounding to a Multiple
In the following example, we round an array of values to the nearest multiple of 0.5 using NumPy's around() function −
import numpy as np # Define an array of values values = np.array([3.14159, 2.71828, 1.61803, 0.57721]) # Round each element to the nearest multiple of 0.5 rounded_multiple_values = np.around(values * 2) / 2 print("Rounded to nearest multiple of 0.5:", rounded_multiple_values)
The output obtained is as shown below −
Rounded to nearest multiple of 0.5: [3. 2.5 1.5 0.5]