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?

Anas
  • 5,622
  • 5
  • 39
  • 71
gotqn
  • 42,737
  • 46
  • 157
  • 243
  • 1
    What do you get if you do: `Reponse.Write TypeName(Score)`? – Jon Egerton Aug 20 '12 at 14:32
  • 1
    Well, that's very strange - i get nothing. So, I suppose that's why the function did not work. – gotqn Aug 20 '12 at 14:39
  • 1
    Be aware that Isnumeric() can accept values that you might not exspect. See http://classicasp.aspfaq.com/general/what-is-wrong-with-isnumeric.html (scroll down to vbscript section) – David Aug 21 '12 at 10:22

1 Answers1

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.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
  • 1
    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... – gotqn Aug 20 '12 at 14:36
  • 2
    Probably some fancy type that the driver can't directly understand. – Shadow The GPT Wizard Aug 20 '12 at 14:45
  • 1
    @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? – kneidels Dec 12 '16 at 14:35
  • 1
    @kneidels 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, ",", ".")` – Shadow The GPT Wizard Dec 12 '16 at 15:27
  • 1
    Thanks @ShadowWizard - i dont have that comma int he Db value - i just wrote it that way here to make it more clear – kneidels Dec 12 '16 at 16:30
  • @kneidels so what **exactly** you do get when you `Response.Write` the value? That's very crucial if you want to debug and fix that. – Shadow The GPT Wizard Dec 12 '16 at 16:36
  • @ShadowWizard. i get `500000` – kneidels Dec 12 '16 at 23:04
  • Yes :-) I just cant understand why `bigint` isnt understood by Vbscript – kneidels Dec 13 '16 at 11:11
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/130500/discussion-between-shadow-wizard-and-kneidels). – Shadow The GPT Wizard Dec 13 '16 at 11:40