_CAST

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search

The _CAST function performs a C-like cast of a numerical value to a specified numerical type.


Syntax

result = _CAST(numericalType, numericalValue)


Parameters


Description

  • _CAST allows explicit type conversion, similar to C-style casting.
  • _CAST does not round the value like INT, _ROUND, CINT, or CLNG.
  • The result type is determined by numericalType.
  • A compiler error is thrown if numericalType is invalid or numericalValue is a STRING.
  • No runtime errors are thrown if numericalType and numericalValue are valid.


Availability


Examples

Example 1
Basic type conversion.
DIM a AS DOUBLE
DIM b AS _INTEGER64

a = 123.456
b = _CAST(_INTEGER64, a)

PRINT "Original: "; a
PRINT "Casted: "; b

a = 456.789
b = _CAST(_INTEGER64, a)

PRINT "Original: "; a
PRINT "Casted: "; b

a = -123.456
b = _CAST(_INTEGER64, a)

PRINT "Original: "; a
PRINT "Casted: "; b

a = -456.789
b = _CAST(_INTEGER64, a)

PRINT "Original: "; a
PRINT "Casted: "; b
Original:  123.456
Casted:  123
Original:  456.789
Casted:  456
Original: -123.456
Casted: -123
Original: -456.789
Casted: -456

Example 2
Type conversion function comparison.
PRINT 4.6!; INT(4.6!); FIX(4.6!); CLNG(4.6!); _ROUND(4.6!); _CAST(LONG, 4.6!)
PRINT -4.6!; INT(-4.6!); FIX(-4.6!); CLNG(-4.6!); _ROUND(-4.6!); _CAST(LONG, -4.6!)

PRINT 1%%; _CAST(_UNSIGNED _BYTE, 1%%)
PRINT -1%%; _CAST(_UNSIGNED _BYTE, -1%%)
 4.6  4  4  5  5  4
-4.6 -5 -4 -5 -5 -4
 1  1
-1  255


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link