问题
In ASP, what is wrong at here :
MyPerfFee = (cdbl(TotforMonth)/100) * cdbl(trim(perfFee))
I get the error :
Microsoft VBScript runtime error '800a000d Type mismatch: 'cdbl' /client_services/admin/commscalc.asp, line 48
回答1:
The problem here is either TotforMonth
or perfFee
are not numeric values as far is VBScript is concerned. When dealing with numeric fields it for an external source (flat file, rdbms etc.) it's always best to check them before attempting computations.
For example
<%
'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
%>
You may want to change CDbl()
to CLng()
or CInt()
depending on what numeric type you wish to work with.
Obviously, if you are going to use this a lot you could also build a function to do it for you, something like;
<%
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
%>
Then use it like this;
<%
TotforMonth = ConvertToNumeric(totforMonth, vbDouble, 0)
perfFee = ConvertToNumeric(perfFee, vbDouble, 0)
%>
Code is provided un-tested
Useful Links
- ASP Classic - Type mismatch: 'CInt' - Easy question
来源:https://stackoverflow.com/questions/55183310/what-is-wrong-here-myperffee-cdbltotformonth-100-cdbltrimperffee