Results 1 to 5 of 5

Thread: check floating point

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Posts
    16

    check floating point

    I want to make an error checking to check whether the value in the textbox is a floating point or not. Please help me...........

  2. #2
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: check floating point

    Hi bfughinelaihl ! Welcome to VBForums.

    Try the following code. I've assumed if a user entered 1., it will be NON-floating point. But if s/he enters 1.0, it will be floating point.

    If you don't want this check, omit the code in bold font.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     MsgBox IsFloatingPoint(Text1.Text)
    5. End Sub
    6.  
    7. Private Function IsFloatingPoint(strText As String) As Boolean
    8.     Dim pos As Long
    9.     If IsNumeric(strText) Then
    10.         pos = InStr(1, strText, ".")
    11.         IsFloatingPoint = (pos > 0) [b]And (pos < Len(strText))[/b]
    12.     End If
    13. End Function
    Last edited by iPrank; Jan 20th, 2007 at 10:19 AM.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Posts
    16

    Re: check floating point

    Thanks a lot.... But what is <pos = InStr(1, strText, ".")> mean ? I cannot find it in books.

  4. #4
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    Re: check floating point

    InStr tests wether one string is present in another string.
    In this case it looks for the decimal separator.

    Note that this will think that "a.b" is a float as well.
    You could add an IsNumeric test for more robustness.

    Another way would be IsFloat = (number <> Fix(number))

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Posts
    16

    Re: check floating point

    How to apply <IsFloat = (number <> Fix(number))> ?
    And how to get the value after the decimal point ?
    Last edited by bfughinelaihl; Jan 20th, 2007 at 09:17 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width