NINT Function
Converts input to the nearest integer.
enabled.
Usage
result = NINT(x)
Input Parameters
x—A scalar or array of any numeric data-type, usually float or double.
Keywords
Long—If present and non-zero, NINT returns a long instead of a short (FIX) integer.
Floatingpoint—If this keyword is present and the input array is of type float or double, the output array is of the same type as the input.
Returned Value
result—A scalar or array of the same dimensions as x, where result(i) is the nearest integer to x(i). result is of type short-integer unless the Long or Floatingpoint keyword has been set.
Discussion
Instead of truncating the input (as FIX does), first the input is rounded by adding or subtracting 0.5 (depending on whether the input is greater or less than zero), and then it is truncated.
If the input is out of the range of integers (for example, if you pass in 1.0d33), an error message will result and NINT returns garbage.
Add ±0.5 to the input and convert that to a short integer using FIX. If the Long keyword is used, it’s converted via long. If the input is a FIX, then it’s just passed back. If it’s a long, it’s also passed back. Strings are converted to bytes before the rounding. In the case of complex values, their magnitude is taken. Structures are not allowed.
Examples
; Round 5.1 to the nearest integer, which is 5.
PRINT, NINT(5.1)
; PV-WAVE prints: 5
 
; Round 5.6 up to 6.
PRINT, NINT(5.6)
; PV-WAVE prints: 6
 
; The nearest integer to -1.9 is -2.
PRINT, NINT(-1.9)
; PV-WAVE prints: -2
 
; An array of input floating point numbers returns a similar 
; array of the nearest integers.
PRINT, NINT([0.1, -20.9, 50.9])
; PV-WAVE prints: 0     -21      51
 
; NINT returns incorrect results when the input is out of 
; the range of integers.
PRINT, NINT(200000.1)
; PV-WAVE prints: 3392
 
; Floating point numbers which are out of the range of fix 
; type integers but in the range of long type integers should 
; be rounded using the Long keyword.
PRINT, NINT(200000.1, /Long)
; PV-WAVE prints: 200000
See Also