 Search:

# Math function to determine if a number is an int

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

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

`Example:Dim A as New Double = 1.234If IsValueInt(A) ThenDoSomething()ElseDoSomethingElse()End If`

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

Share:

This can probably be tweaked to suit your needs:

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

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

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

| | 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 integerElse'Otherwise it is!End If`

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

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" %><html><head><SCRIPT runat=server>Private Sub Page_Load(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles MyBase.LoadDim a as double  = 23.123Dim b as string = "23.123"If IsNumeric(a) and IsNumeric(b) ThenResponse.Write("we equal")ElseResponse.Write("we not equal")End IfEnd Sub</SCRIPT></head><body></body></html>`

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

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

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 trueif (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.