Avatar of Emanuel Morris
Emanuel MorrisFlag for United States of America

asked on 

Visual Basic Classic to check a text box for float

How to check a text box to see if a float has been entered
Visual Basic Classic

Avatar of undefined
Last Comment
Arana (G.P.)
ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
There are no way to differenciate an integer from a floating point (double) value since an integer can be stored in a double.
And forcing users to input the format you expect is not an option (users should not adapt to you).

At best, you can use the IsNumeric function (beware that it is subject to local settings), or use a regular expression:
^0*[1-9]\d*(?:[.,]\d+[1-9])?$     Check for numeric with a point or a coma as decimal separator
^0*[1-9]\d*(?:\.\d+[1-9])?$       Check for numeric with a point as decimal separator
^0*[1-9]\d*(?:,\d+[1-9])?$        Check for numeric with a coma as decimal separator
SOLUTION
Avatar of Arana (G.P.)
Arana (G.P.)

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
@Arana:
Still the same issue, not all floating point numbers have a decimal part.
Your functions fail when given a double without decimal part converted into string as parameter:
Dim Data As String
Data = cstr(cdbl(10))    '// double converted to string

debug.print IsFloat(Data)        '// return False

Open in new window


Avatar of Arana (G.P.)
Arana (G.P.)

Well unless Asker confirms otherwise I believe he wants to know if its a fractional number, because if he wants to check for a float then there is no way to verify it against a string made of numbers(its a string anyway), also float doesn't mean DOUBLE, unless you are refering to SQL Float, and there is no float data type in VB6, if you want the equivalent of C# Float then it will be VB SINGLE not DOUBLE.

CNT(10): 10 would fit in an int, why would you convert 10 to double? , and not a float for sure, unless you specifically assign it to a double data type.

if you want to check against a specific data type, and not only see if your value has a decimal portion then you can use
the vartype function you can use it to specifically check for 1 type like in:

Private Function IsSingle(ByVal value As Variant) As Boolean
    IsSingle =VarType(value) = vbSingle)
End Function

By the way, VB6 does not support regexp unless you call VBA library and all those regexp fail if your last digit is a zero. (ie 101.110) , I know 0 to the right shouldn't matter but as you said you cant force user to not send that as a parameter.
Visual Basic Classic
Visual Basic Classic

Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.

165K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo