- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Round to nearest integer towards zero in Python

To round to nearest integer towards zero, use the numpy.fix() method in Python Numpy. It rounds an array of floats element-wise to nearest integer towards zero. The rounded values are returned as floats. The 1st parameter, x is an array of floats to be rounded. The 2nd parameter, out is a location into which the result is stored. If provided, it must have a shape that the input broadcasts to. If not provided or None, a freshly-allocated array is returned.

The method returns a float array with the same dimensions as the input. If second argument is not supplied then a float array is returned with the rounded values. If a second argument is supplied the result is stored there. The return value out is then a reference to that array.

## Steps

At first, import the required library −

import numpy as np

Create an array with float type using the array() method −

arr = np.array([120.6, -120.6, 200.7, -320.1, 320.1, 500.6])

Displaying our array −

print("Array...

",arr)

Get the datatype −

print("

Array datatype...

",arr.dtype)

Get the dimensions of the Array −

print("

Array Dimensions...

",arr.ndim)

Get the number of elements of the Array −

print("

Number of elements in the Array...

",arr.size)

To round to nearest integer towards zero, use the numpy.fix() method in Python Numpy. It rounds an array of floats element-wise to nearest integer towards zero. The rounded values are returned as floats −

print("

Result (rounded)...

",np.fix(arr))

## Example

import numpy as np # Create an array with float type using the array() method arr = np.array([120.6, -120.6, 200.7, -320.1, 320.1, 500.6]) # Display the array print("Array...

", arr) # Get the type of the array print("

Our Array type...

", arr.dtype) # Get the dimensions of the Array print("

Our Array Dimension...

",arr.ndim) # Get the shape of the Array print("

Our Array Shape...

",arr.shape) # To round to nearest integer towards zero, use the numpy.fix() method in Python Numpy # It rounds an array of floats element-wise to nearest integer towards zero. The rounded values are returned as floats. print("

Result (rounded)...

",np.fix(arr))

## Output

Array... [ 120.6 -120.6 200.7 -320.1 320.1 500.6] Our Array type... float64 Our Array Dimension... 1 Our Array Shape... (6,) Result (rounded)... [ 120. -120. 200. -320. 320. 500.]

- Related Questions & Answers
- How to round down to nearest integer in MySQL?
- Round elements of the array to the nearest integer in Numpy
- Round seconds to nearest half minute in MySQL?
- How to round to the nearest hundred in R?
- How to round up to the nearest N in JavaScript
- Round number down to nearest power of 10 JavaScript
- Round a number to the nearest even number in C#
- How to round the decimal number to the nearest tenth in JavaScript?
- Mean of an array rounded down to nearest integer in JavaScript
- How to round correlation values in the correlation matrix to zero decimal places in R?
- What is the shortest way to round to the integer value in R?
- How to convert an integer to string with padding zero in C#?
- Moving Towards Artificial Intelligence
- Return the next floating-point value after a zero-dimensional array value towards another value in Numpy
- Guess Nearest Square Root in Python