Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IsNumeric is not working 4

Status
Not open for further replies.

yuli1104

Programmer
Feb 20, 2002
60
CA
Hello,
Is there anything wrong with IsNumeric() function in VB6.
Isnumeric() is used in many applications of our company. It seems not working properly.
For example, 181D59 is not a numeric,when isnumeric(181D59) returns true, it is causing problem to our application.
For example, the messagebox of "yes" will popup and that is not right (it is just an example, the code is not like this in our application)
Anybody has encountered such a problem ? is there any patch to fix it ? Thanks
=========================================================
Private Sub Form_Load()
Dim a As Variant
a = "181D59"
If IsNumeric(a) Then
MsgBox "yes"
Else
MsgBox "NO"
End If
End Sub
 
Well I just opened up the windows calculator, set it to scientific mode, clicked the Hex radio button, typed in 181D59 and then clicked the Dec radio button.... the result was 1580377.
 
As Sheco pointed out, 181D59 is a number. It just happens to be a hexadecimal number. You may need to use a regular expression to check for the existence of letters before using IsNumeric.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Um ... it is a feature

D (and E) are lesser known/undocumented ways of providing an exponent

If you saw 181E+59 you'd agree that it was an (exponential) number, yes? And that IsNumber("181E+59") should theefore return True, yes?

Well... E is an alternative for E+, and in VB exponential numbers E can always be replaced with D, so:

181E+59 = 181E59 = 181D+59 = 181D59 (=1.81E+61)

All of the above are numbers as far as VB (and therefore the IsNumeric function) is concerend
 
I just tried using other hex characters A, B, C and F and they all cause IsNumeric() to fail.

Oh well, there is no shame in getting schooled by strongm

Have a star!
 
If you want a fuller explanation it is that E stands for exponent and D stands for double-precision exponent (which theoretically provides more accuracy than the regular exponent: 24-bit precision versus 53-bit in the mantissa => approx 7 decimal digits against approx 15 decimal digits)
 
I stand schooled as well. Although my suggestion is still valid. :)

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
strongm,

How do you find these things out if they are "undocumented"?

>>D (and E) are lesser known/undocumented ways of providing an exponent
 
Visual Basic documentation does not mention anything about the letter D. If we code [tt]X = 1D20[/tt] it is automatically converted to [tt]X = 1E+20[/tt] when VB compiles the code. It means that VB does recognize the letter D as exponent, but at the same time, it insists to use the letter E for this purpose.

As far as their history is concerned, I believe they are inherited from QuickBASIC/QBasic where these two types of exponents were distinguished from each other.

I just switched on QB (after a long time) and found the following content from the help in which these two exponents are clearly distinguished based on the data type as strongm mentioned.
[tt]
Limits to QuickBASIC - Names, Strings, and Numbers
Maximum Minimum
Variable name length 40 characters 1 character
String length 32,767 characters 0 characters
Integers 32,767 -32,768
Long Integers 2,147,483,647 -2,147,483,648
Single precision numbers (positive) 3.402823 E+38 1.401298 E-45
Single precision numbers (negative) -1.401298 E-45 -3.402823 E+38
Double precision numbers (positive)
Maximum: 1.797693134862315 D+308
Minimum: 4.940656458412465 D-324
Double precision (negative)
Maximum: -4.940656458412465 D-324
Minimum: -1.797693134862315 D+308[/tt]
 
Great bit of history - Thanks Hypetia

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first
'If we're supposed to work in Hex, why have we only got A fingers?'
Essex Steam UK for steam enthusiasts
 
You can keep everything but numerals out of a textbox like this. It doesn't even allow decimal points!
Code:
Option Explicit

Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const GWL_STYLE = (-16)
Private Const ES_NUMBER As Long = &H2000& 'or 8192 in decimal form

Private Sub Form_Load()
    Dim style As Long
    Text1.Text = ""
    style = GetWindowLong(Text1.hwnd, GWL_STYLE)
    SetWindowLong Text1.hwnd, GWL_STYLE, style Or ES_NUMBER
End Sub

David
 
> It doesn't even allow decimal points!

Doesn't even allow negatives!

>You can keep everything but numerals out of a textbox like this

Not quite. It does not prevent non-numeric values from being copied into the control. So you still need to Validate the control.

 
How about just using the following???

Private Sub Command1_Click()
Dim a As Variant

a = "181D59"
For i = 1 To Len(a)
If Asc(Mid$(a, i, 1)) < 48 Or Asc(Mid$(a, i, 1)) > 57 Then
MsgBox "Not A Number"
Exit Sub
End If
Next i
MsgBox "Yes It Is A Number"

End Sub
 
The variation:

Private Sub Text4_Change()
Dim i As Integer
Dim strText As String

strText = Text4.Text

For i = Len(strText) To 1 Step -1
If Asc(Mid$(strText, i, 1)) < 48 Or Asc(Mid$(strText, i, 1)) > 57 Then
Text4.Text = Replace(Text4.Text, Mid$(strText, i, 1), vbNullString)
Text4.SelStart = Len(Text4.Text)
End If
Next i

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top