IsNumeric function not working correctly
Asked Answered
Q

1

8

I have very strange problem with IsNumeric function in classic asp fail. Something like this happens in my code:

Response.write Score                // 79.617
Response.write IsNumeric(Score)     // false
Response.write IsNumeric("79.617")  // true

Has anyone had an idea why this could happen?

In the specifications it is said that the functions works with strings that can be converted into numbers, and from the example above you can see i get "true" result. But what can then cause my issue?

Quash answered 20/8, 2012 at 14:24 Comment(3)
What do you get if you do: Reponse.Write TypeName(Score)?Churl
Well, that's very strange - i get nothing. So, I suppose that's why the function did not work.Quash
Be aware that Isnumeric() can accept values that you might not exspect. See classicasp.aspfaq.com/general/what-is-wrong-with-isnumeric.html (scroll down to vbscript section)Defiant
W
11

This means Score is simply not a string but rather something else, most likely coming from database.

To be on the safe side, use your own function:

Function My_IsNumeric(value)
    My_IsNumeric = False
    If IsNull(value) Then Exit Function
    My_IsNumeric = IsNumeric(CStr(value))
End Function

Response.write My_IsNumeric(Score)

The CStr() will make sure to convert anything except Null to a string, and to handle Null coming from database you have the IsNull() function.

Waggoner answered 20/8, 2012 at 14:29 Comment(9)
This helps to solve my problem. You are right that I get the score from database. I get a object from the database, but I still can not understand why the result is not a string...Quash
Probably some fancy type that the driver can't directly understand.Waggoner
@ShadowWizard - same issue by me. I am getting the number 500,000 from a database and isnumeric() is returning false. database field type is bigint - is isnumeric not able to understand this? Also - are there any dangers with converting to a string and then back to int?Lindquist
@Lindquist your case appears to be due to the comma: in the database settings, the decimal separator is a comma, while on the server where the classic ASP code is located, it's period. So yet again, better use a custom function, and in there convert comma to period: value = Replace(value, ",", ".")Waggoner
Thanks @ShadowWizard - i dont have that comma int he Db value - i just wrote it that way here to make it more clearLindquist
@Lindquist so what exactly you do get when you Response.Write the value? That's very crucial if you want to debug and fix that.Waggoner
@ShadowWizard. i get 500000Lindquist
Yes :-) I just cant understand why bigint isnt understood by VbscriptLindquist
Let us continue this discussion in chat.Waggoner

© 2022 - 2024 — McMap. All rights reserved.