CINT
- 来源: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgCint
- 最后更新: 2016-11-07
将数值或字符串表达式转换为 Integer 或 Integer<bits>
语法
vb
declare function Cint ( byval expression as datatype ) as integer
declare function Cint`<bits>` ( byval expression as datatype ) as integer`<bits>`
Type typename
declare operator cast ( ) as integer
declare operator cast ( ) as integer`<bits>`
End Type用法
vb
result = Cint( expression )
result = Cint( string expression )
result = Cint( user defined type )参数
bits
表示所需整数位数的数值常量表达式。允许的值为 8、16、32 或 64。
expression
要转换为 Integer 值的数值、字符串或指针表达式
datatype
任何数值、字符串或指针数据类型
typename
用户自定义类型
返回值
包含转换后值的 Integer 或 Integer<bits>。
说明
如果向 Cint 传递数值 expression,它会使用银行家舍入法进行舍入——即舍入到最接近的整数值,若数字与两个整数等距则选择最近的偶数整数——并返回 Integer;若提供了 bits 值,则返回给定大小的整数类型。
该函数不检查溢出;例如,对于 32 位 Integer,小于 -2147483648 或大于 2147483647 的值结果是未定义的。
如果参数是字符串表达式,则根据结果类型的大小使用 Valint 或 Vallng 将其转换为数值。
函数名 "CINT" 来源于"转换为整数"(Convert to INTeger)。
示例
start GeSHi
vb
' Using the CINT function to convert a numeric value
'Create an INTEGER variable
Dim numeric_value As Integer
'Convert a numeric value
numeric_value = CInt(300.5)
'Print the result, should return 300, because 300 is even
numeric_value = CInt(301.5)
'Print the result, should return 302, because 301 is odd
Print numeric_valueend GeSHi
方言差异
- 在 -lang qb 方言中,
Cint将返回 16 位整数,与 QB 相同。
与 QB 的差异
- QB 不允许字符串参数
- QB 不允许
<bits>参数
另请参阅
CastCbyteCubyteCshortCushortCuintClngCulngClngintCulngintCsngCdblInteger
返回 目录