MyPerfFee =(cdbl(TotforMonth)/ 100)* cdbl(trim(perfFee))是怎么回事?

时间:2019-03-15 13:06:08

标签: vbscript asp-classic

在ASP中,这里有什么问题:

MyPerfFee = (cdbl(TotforMonth)/100) * cdbl(trim(perfFee))

我收到错误消息:

  

Microsoft VBScript运行时错误'800a000d类型不匹配:'cdbl'   /client_services/admin/commscalc.asp,第48行

1 个答案:

答案 0 :(得分:1)

就VBScript而言,这里的问题是TotforMonthperfFee不是数值。在处理数字字段时,应将其用于外部源(平面文件,rdbms等)。始终最好在进行计算之前检查它们。

例如

<%
'These values are to be used in a calculation quickly make sure we are dealing with numeric values.
If Len(TotforMonth) > 0 And IsNumeric(TotforMonth) Then TotforMonth = CDbl(TotforMonth) Else TotforMonth = 0
If Len(perfFee) > 0 And IsNumeric(perfFee) Then perfFee = CDbl(perfFee) Else perfFee = 0
%>

您可能要根据要使用的数字类型将CDbl()更改为CLng()CInt()

很明显,如果您打算经常使用它,您还可以构建一个函数来为您完成此操作,例如;

<%
Function ConvertToNumeric(value, type, defaultValue)
  If Len(value) > 0 And IsNumeric(value) Then
    Select Case type
    Case vbDouble
      value = CDbl(value)
    Case Else
      '...
    End Select
  Else
    value = defaultValue
  End If
  ConvertToNumeric = value
End Function
%>

然后像这样使用它;

<%
TotforMonth = ConvertToNumeric(totforMonth, vbDouble, 0)
perfFee = ConvertToNumeric(perfFee, vbDouble, 0)
%>

代码未经测试提供


有用链接

相关问题