# is there any is integer function ?

Discussion in 'ASP .Net' started by Tee, Feb 27, 2004.

1. ### TeeGuest

Hi,

how do we detect if a value is a positive integer ?

thanks,
Tee

Tee, Feb 27, 2004

2. ### TJSGuest

if myValue > 0 then ...

"Tee" <thy[NO@SPAM]streamyx.com> wrote in message
news:%23eHvYdO\$...
> Hi,
>
> how do we detect if a value is a positive integer ?
>
> thanks,
> Tee
>
>

TJS, Feb 27, 2004

3. ### Scott Mitchell [MVP]Guest

> how do we detect if a value is a positive integer ?

Are you using VB.NET or C#? In C# I'd do:

if (x is int)
{
if ((int) x) > 0)
// x is a positive integer
else
// x is a negative integer
}
else
// x is NOT an integer at all!

In VB.NET you can use IsNumeric().

--

Scott Mitchell

http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!

Scott Mitchell [MVP], Feb 27, 2004
4. ### Scott M.Guest

And how would this know the difference between 1 and 1.1?

if x > 0 AND x = CTYPE(x, Integer) then

"TJS" <> wrote in message
news:%23M7RetO\$...
> if myValue > 0 then ...
>
>
> "Tee" <thy[NO@SPAM]streamyx.com> wrote in message
> news:%23eHvYdO\$...
> > Hi,
> >
> > how do we detect if a value is a positive integer ?
> >
> > thanks,
> > Tee
> >
> >

>
>

Scott M., Feb 27, 2004
5. ### TeeGuest

Hi,

I know about the isnumeric, but ... whatever number value will return true
with isnumeric, so I hope to get something like isInteger() ...

thanks.
Tee

"Scott Mitchell [MVP]" <> wrote in message
newsSz%b.30990\$...
>> how do we detect if a value is a positive integer ?

>
> Are you using VB.NET or C#? In C# I'd do:
>
> if (x is int)
> {
> if ((int) x) > 0)
> // x is a positive integer
> else
> // x is a negative integer
> }
> else
> // x is NOT an integer at all!
>
>
> In VB.NET you can use IsNumeric().
>
> --
>
> Scott Mitchell
>
> http://www.4GuysFromRolla.com
> http://www.ASPFAQs.com
> http://www.ASPMessageboard.com
>
> * When you think ASP, think 4GuysFromRolla.com!

Tee, Feb 27, 2004
6. ### TeeGuest

Great idea,

Thanks.

"Scott M." <> wrote in message
news:uidueTP\$...
> And how would this know the difference between 1 and 1.1?
>
> if x > 0 AND x = CTYPE(x, Integer) then
>
>
> "TJS" <> wrote in message
> news:%23M7RetO\$...
>> if myValue > 0 then ...
>>
>>
>> "Tee" <thy[NO@SPAM]streamyx.com> wrote in message
>> news:%23eHvYdO\$...
>> > Hi,
>> >
>> > how do we detect if a value is a positive integer ?
>> >
>> > thanks,
>> > Tee
>> >
>> >

>>
>>

>
>

Tee, Feb 27, 2004
7. ### Harsh ThakurGuest

Hi,

As noted before, for a positive integer, you can just say:
if(x > 0)
To test whether x is in integer, you can say:
x = int.Parse(value.ToString())
where value is the number you want to check.

If value represents an integer, x will contain it after
the step given above. Else an FormatException will be
thrown.

For more details you can check out int.Parse in MSDN.

HTH
Regards
Harsh Thakur

>-----Original Message-----
>Hi,
>
>how do we detect if a value is a positive integer ?
>
>thanks,
>Tee
>
>
>.
>

Harsh Thakur, Feb 27, 2004
8. ### Kevin SpencerGuest

> with isnumeric, so I hope to get something like isInteger() ...

You need to re-read Scott's reply. That was the first thing he tested for.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
of lots of little things.

"Tee" <thy[NO@SPAM]streamyx.com> wrote in message
news:uTcdEYP\$...
> Hi,
>
> I know about the isnumeric, but ... whatever number value will return true
> with isnumeric, so I hope to get something like isInteger() ...
>
> thanks.
> Tee
>
>
>
> "Scott Mitchell [MVP]" <> wrote in message
> newsSz%b.30990\$...
> >> how do we detect if a value is a positive integer ?

> >
> > Are you using VB.NET or C#? In C# I'd do:
> >
> > if (x is int)
> > {
> > if ((int) x) > 0)
> > // x is a positive integer
> > else
> > // x is a negative integer
> > }
> > else
> > // x is NOT an integer at all!
> >
> >
> > In VB.NET you can use IsNumeric().
> >
> > --
> >
> > Scott Mitchell
> >
> > http://www.4GuysFromRolla.com
> > http://www.ASPFAQs.com
> > http://www.ASPMessageboard.com
> >
> > * When you think ASP, think 4GuysFromRolla.com!

>
>

Kevin Spencer, Feb 27, 2004
9. ### JesseChallis

Joined:
Jul 1, 2010
Messages:
1
0
A different Approach

public bool IsNumber(string text)
{
bool result = true;
foreach (var c in text)
{
if (!Char.IsDigit(c))
{
result = false;
break;
}
}

return result;
}

Cheers

JesseChallis, Jul 1, 2010
10. ### rutgers

Joined:
Feb 28, 2011
Messages:
1
0
Try this:

If IsNumeric(UserInputTextBox.Text) And (System.Math.Abs(val(UserInputTextBox.Text) Mod 1) <> 0) Then
'______Do something
End if

************************
OR Atfer checking IsNumeric(UserInputTextBox.Text)
Do this following:

if (System.Math.Abs(val(UserInputTextBox.Text) Mod 1) <> 0)

Last edited: Feb 28, 2011
rutgers, Feb 28, 2011
11. ### kmoloney

Joined:
Jan 27, 2012
Messages:
1
0
Positive Integer Function in vb.net

In vb.net you get a cast error if you pass a string in some functions. This should take care of it:

Private Function IsPositiveInteger(ByVal objValue AsObject) As Boolean
Dim intX AsInteger
If Not (IsNumeric(objValue)) Then
Return False
Else
intX = CInt(objValue)
If objValue > 0 Then
Return True
Else
Return False
End If
End If

Last edited: Jan 27, 2012
kmoloney, Jan 27, 2012

Joined:
Jan 30, 2012
Messages:
1