Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

Math function to determine if a number is an int

  Asked By: Vivek    Date: Aug 03    Category: Asp.net    Views: 2479

Does anyone know how to determine if a given number is an integer
without using string functions?


Dim A as New Double = 1.234
If IsValueInt(A) Then
End If

I can't quite think of the right Math function to use.



7 Answers Found

Answer #1    Answered By: Julia Flores     Answered On: Aug 03

This can probably be tweaked to suit your needs:

// from
// http://www.devcity.net/forums/topic.asp?tid=7267
public static bool isNumeric(object val)
//string val = value.ToString();
double d = System.Double.Parse(val.ToString(),
return true;
catch (System.Exception)
return false;

Answer #2    Answered By: Jarvia Miller     Answered On: Aug 03

use keyword typeof and determine  type of some object.
or use object.GetType() method.

Answer #3    Answered By: Allan Bailey     Answered On: Aug 03

or if you're planning to use vb - you can just call the IsNumeric( )

Answer #4    Answered By: Baylen Smith     Answered On: Aug 03

| | Does anyone know how to determine  if a given number  is an integer
| | without using string  functions?
| or if you're planning to use vb - you can just call the IsNumeric( )
| function

But IsNumeric will return true for both 3.14 and 3, while 3 is an
integer, but 3.14 is not.

One way to determine if a number is an Integer is to do something like:

If Convert.ToInt32(x) <> x then
'x is NOT an integer
'Otherwise it is!
End If

This essentially truncates the value of x and then determines if the
truncated form equals the original form.

Answer #5    Answered By: Lughaidh Fischer     Answered On: Aug 03

I think IsNumeric( ) will handle doubles as well - I don't think it requires
an integer to return true.... for example - this test page will print out
"we equal" if b is equal to "23.123" or "23" (same as if a is equal to
23.123 or 23) -- if you pop a character in the string  for b, then "we not
equal" gets printed....

<%@ Page Language="vb" AutoEventWireup="false" %>
<SCRIPT runat=server>
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim a as double  = 23.123
Dim b as string = "23.123"
If IsNumeric(a) and IsNumeric(b) Then
Response.Write("we equal")
Response.Write("we not equal")
End If
End Sub

Answer #6    Answered By: Aalia Arain     Answered On: Aug 03

function  that
will return true if there are no decimal points and false if there

Maybe a little futher explanation would help. I'm trying to
determine if give number  is a power of 2. The math  function log
does this very nicely, but the determination is wheather or not the
function returns an integer or not. For instance:

Log(4,2) = 2 which means that 4 is a power of 2
Log(5,2) = 2.3219... which means that 5 is not a poewr of 2

I originally converted the result to a string  and then checked to
see if a decimal point existed using the indexOf method, but I
didn't like it as I felt the data conversion and searching through a
string might cause a bit of a performance hit.

What I was _REALLY_ hoping to get was a math function, but the
answer Scott provided is sufficient.

Answer #7    Answered By: Terence Mitchell     Answered On: Aug 03

Just for the sake of presenting another idea - you could also use the mod
keyword so that

if (Log(4,2) mod 1 = 0)
'this would execute since it's true

if (Log(5,2) mod 1 = 0)
'this would not since it's false

Didn't find what you were looking for? Find more on Math function to determine if a number is an int Or get search suggestion and latest updates.