将字符串转换为整数 .asp 经典 [英] Convert string to integer .asp classic

查看:17
本文介绍了将字符串转换为整数 .asp 经典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,注释详细说明了应该发生的情况:

I have the following code, comments detail what should happen:

averageNum = myArray2(0) 'assign variable
response.write(TypeName(averageNum)&"<br>") 'check var type: string as expected
averageNum = CInt(averageNum) 'convert to integer

当我运行时,我得到了

Type mismatch: 'CInt'

我需要将变量转换为整数,因为我需要使用它进行计算

I need to turn the variable into an integer as I need to perform calculations with it

推荐答案

我会检查 myArray2(0) 的值是否是你所期望的整数.执行此操作的简单方法是使用返回 Boolean 值的 IsNumeric().

I would be checking that the value of myArray2(0) is an integer as you're expecting. Simple way to do this is using IsNumeric() which returns a Boolean value.

类似的东西;

averageNum = myArray2(0) 'assign variable
'Check numeric value assume 0 if not numeric.
If Len(averageNum) > 0 And IsNumeric(averageNum) Then
  averageNum = CInt(averageNum) 
Else
  averageNum = 0
End If

这篇关于将字符串转换为整数 .asp 经典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆