I want to make an error checking to check whether the value in the textbox is a floating point or not. Please help me........... :cry:
Printable View
I want to make an error checking to check whether the value in the textbox is a floating point or not. Please help me........... :cry:
Hi bfughinelaihl ! Welcome to VBForums. :wave:
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:
Option Explicit Private Sub Command1_Click() MsgBox IsFloatingPoint(Text1.Text) End Sub Private Function IsFloatingPoint(strText As String) As Boolean Dim pos As Long If IsNumeric(strText) Then pos = InStr(1, strText, ".") IsFloatingPoint = (pos > 0) [b]And (pos < Len(strText))[/b] End If End Function
Thanks a lot.... But what is <pos = InStr(1, strText, ".")> mean ? I cannot find it in books.
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))
How to apply <IsFloat = (number <> Fix(number))> ?
And how to get the value after the decimal point ? :confused: