how to determine if numeric data contains a decimal?

G

Guest

Is there a way to determine if a numeric value contains a decimal? how to do
this?

If IsNumeric(txt1.Text) then ... so now I know that txt1.Text is a numeric
value. How can I tell if it is a plain integer or decimal?

Thanks,
Rich
 
R

rowe_newsgroups

Is there a way to determine if a numeric value contains a decimal? how to do
this?

If IsNumeric(txt1.Text) then ... so now I know that txt1.Text is a numeric
value. How can I tell if it is a plain integer or decimal?

Thanks,
Rich

Parse the string for a period using IndexOf.

<pseudocode>

if IsNumeric(txt1.Text) then
if txt1.IndexOf(".") <> -1 then
' Decimal
else
' Integer
end if
end if

</pseudocode>

Thanks,

Seth Rowe
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

rowe_newsgroups said:
Parse the string for a period using IndexOf.

<pseudocode>

if IsNumeric(txt1.Text) then
if txt1.IndexOf(".") <> -1 then
' Decimal
else
' Integer
end if
end if

</pseudocode>

Thanks,

Seth Rowe

That's assuming that the decimal separator is a period, which it isn't
everywhere.

The decimal separator for any given culture can be found in the
NumberFormat.NumberDecimalSeparator property.
 
P

Paul Evans

Göran Andersson said:
That's assuming that the decimal separator is a period, which it isn't
everywhere.

The decimal separator for any given culture can be found in the
NumberFormat.NumberDecimalSeparator property.

Couldn't you also do something along the lines of:

If IsNumeric(txt1.Text) Then
If CLng(txt1.Text) / 1 = CLng(txt1.Text) \ 1 Then
'Integer
Else
'Decimal
End If
End If

Or, if you're doing this a lot and making a decision based on it...

Public Function IsReal (ByVal Number As Object) As Integer
Dim i As Integer
i = -1 'Fail
If IsNumeric(Number) Then
If CLng(Number) / 1 = CLng(Number) \ 1 Then
i = 0 'Integer
Else
i = 1 'Real
End If
End If
Return i
End Function

If IsReal(txt1.Text) < 1 Then MessageBox.Show("I want a real!!!")
If IsReal(txt2.Text) <> 0 Then MessageBox.Show("I want an integerl!!!")

I've just realised that this wouldn't work for "4.000000", but I'll let you
have it anyway. :\
 
G

Guest

I guess there is no builtin mechanism for doing this. I really meant to
ask if there was a builtin mechanism for doing this. Appears not.

Thanks all for your replies.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top