We help IT Professionals succeed at work.

Visual Basic Classic to check a text box for float

Emanuel Morris
on
50 Views
Last Modified: 2020-09-23
How to check a text box to see if a float has been entered
Comment
Watch Question

Social distance - Don't touch your face - Wash your hands for 20 seconds
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Lee W, MVPTechnology and Business Process Advisor
CERTIFIED EXPERT
Most Valuable Expert 2013
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Fabrice LambertConsulting
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
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
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Fabrice LambertConsulting
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
@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

CERTIFIED EXPERT

Commented:
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.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.