Q(uick)BASIC Function: CINT
Quick View
CINT
A conversion function that converts a numeric expression to an integer by rounding the fractional part of the expression
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- CINT(numeric-expression)
- CLNG(numeric-expression)
Description/Parameter(s)
CINT rounds a numeric expression to an integer. CLNG rounds a numeric expression to a long (4-byte) integer.
numeric-expression | For CINT, any numeric expression in the range -32,768 through 32,767. |
For CLNG, any numeric expression in the range -2,147,483,648 through 2,147,483,647. |
Example
PRINT CINT(12.49), CINT(12.51) 'Output is: 12 13
PRINT CLNG(338457.8) 'Output is: 338458
See also:
Syntax
- CINT(numeric-expression)
Description/Parameter(s)
numeric-expression | Any numeric expression between -32,768 and 32,767, inclusive. |
If numeric-expression is not in the range -32,768 to 32,767, the function produces a run-time error message that reads "Overflow."
INT differs from the FIX and INT functions, which truncate, rather than round, the fractional part. See the example for the INT function for an illustration of the differences among these functions.
Example
The following example converts an angle in radians to an angle in degrees and minutes:
'Set up constants for converting radians to degrees.
CONST PI=3.141593, RADTODEG=180./PI
INPUT "Angle in radians = ",Angle 'Get the angle in radians.
Angle = Angle * RADTODEG 'Convert radian input to degrees.
Min = Angle - INT(Angle) 'Get the fractional part.
'Convert fraction to value between 0 and 60.
Min = CINT(Min * 60)
Angle = INT(Angle) 'Get whole number part.
IF Min = 60 THEN '60 minutes = 1 degree.
Angle = Angle + 1
Min = 0
END IF
PRINT "Angle equals" Angle "degrees" Min "minutes"
Sample Output:
Angle in radians = 1.5708 Angle equals 90 degrees 0 minutesSyntax
- CINT(numeric-expression)
Description/Parameter(s)
numeric-expression | Any numeric expression between -32,768 and 32,767, inclusive. |
- If numeric-expression is not between -32,768 and 32,767, inclusive, BASIC generates the run-time error message, "Overflow."
Usage Notes
- CINT differs from the FIX and INT functions, which truncate, rather than round, the fractional part. See the example for the INT function for an illustration of the differences among these functions.
Example
This example compares output from INT, CINT, and FIX, the three functions that convert numeric data to integers.
CLS 'Clear screen.
PRINT " N", "INT(N)", "CINT(N)", "FIX(N)": PRINT
FOR I% = 1 TO 6
READ N
PRINT N, INT(N), CINT(N), FIX(N)
NEXT
DATA 99.3, 99.5, 99.7, -99.3, -99.5, -99.7
Sample Output:
N INT(N) CINT(N) FIX(N) 99.3 99 99 99 99.5 99 100 99 99.7 99 100 99 -99.3 -100 -99 -99 -99.5 -100 -100 -99 -99.7 -100 -100 -99