# how to do isfloat function

Is there any built-in function to detect whether the input is floatting number ?
If no, how to write this function which return true if the input is floating number, otherwise return false.

Thanks !
LVL 1
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
VB supports floating point numbers in the Single, Double, Currency, and Variant data types.  If you only want to see if there is a fractional portion of a number you can use:

Dim vFloat
vFloat = 1234.567
If Int(vFloat) <> vFloat Then
Debug.Print "Floating point"
Else
Debug.Print "No Float"
End If
0
Author Commented:
What if I takes input from a textbox ?
0
Commented:
function isFloat(toTest as String) as boolean
dim i as integer
i=instr(1,toTest,".",vbTextCompare)
if i>0 then
isFloat=true
else
isFloat=false
endif
end function

should work if ur only checking to see if there's a . in the number and since it's a text box, add some code to block the user from entering anything besides numbers and .
0
Commented:
Dim vFloat
vFloat = Val(Text1.Text)
If Int(vFloat) <> vFloat Then
Debug.Print "Floating point"
Else
Debug.Print "No Float"
End If
0
progCommented:
A float cAN HOLD THE NUMBERS

3.14, 3, 0 etc so why not just use isnumeric
0
Commented:
Just check your variable type. If it's a single or a double, it's a float.
0
Commented:
Erick, gkanaan, you're wrong. Currency is not a float. The point does not float, it's fixed. In other words, the point always demarcates the transition from integer to tenths.

In a floating point number the decimal point can mean anything:

1.5 x 10^5 -> here the point separates 100s of thousands from 10s of thousands.
0
Author Commented:
Erick37 is right, this is working, please post yours as asnwer :

Dim vFloat
vFloat = Val(Text1.Text)
If Int(vFloat) <> vFloat Then
Debug.Print "Floating point"
Else
Debug.Print "No Float"
End If

but what does this mean ?

If Int(vFloat) <> vFloat Then
0
Commented:
If Int(vFloat) <> vFloat Then

This compares the Integer portion of the number to the number.  So if the number were 123.45, you will compare 123 with 123.45 (not equal, so there is a non-zero fractional value).
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
0
Commented:
We are *assuming* that the user is not entering values like 1.5 E+5, which evaluates to 150000.  The term "floating point" when talking about text input is meaningless.  As you said, we have to know the data type in order to determine if the number is a float.  The number 1 can be stored as just about any data type, so how do we determine if it is a float?  We cannot.

My interpretation of the question was how to determine if there is a non-zero fractional part of a number.  The code should have read:

Dim vNumber
vNumber = Val(Text1.Text)
If Int(vNumber) <> vNumber Then
Debug.Print "Fractional"
Else
Debug.Print "Whole"
End If
0
progCommented:
I would point out that the number

1.0 is a valid float, but int(1.0) = 1.0, so it would be rejected from being a float.

I think that "1" is a float when converted using val

But I agree that we are just arguing semantics when talking about strings.

USE

If IsNumeric(Text1) Then
MsgBox "float"
End If

0
Commented:
Yep, put like that I'll agree with deighton.
0
Author Commented:
But this :

If IsNumeric(Text1) Then
MsgBox "float"
End If

will allow both int and single. What I want is only to dect a float value like single, so I will award the point to Erick37
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.