[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2164
  • Last Modified:

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 !
0
tanc02
Asked:
tanc02
  • 4
  • 4
  • 3
  • +2
1 Solution
 
Erick37Commented:
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
 
tanc02Author Commented:
What if I takes input from a textbox ?
0
 
gkanaanCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Erick37Commented:
Dim vFloat
vFloat = Val(Text1.Text)
If Int(vFloat) <> vFloat Then
    Debug.Print "Floating point"
Else
    Debug.Print "No Float"
End If
0
 
deightonCommented:
A float cAN HOLD THE NUMBERS

3.14, 3, 0 etc so why not just use isnumeric
0
 
caraf_gCommented:
Just check your variable type. If it's a single or a double, it's a float.
0
 
caraf_gCommented:
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
 
tanc02Author 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
 
Erick37Commented:
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
 
caraf_gCommented:
Whoa! read my comment!
0
 
Erick37Commented:
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
 
deightonCommented:
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
 
caraf_gCommented:
Yep, put like that I'll agree with deighton.
0
 
tanc02Author 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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now