how to check whether a number is an integer?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • André

    #1

    how to check whether a number is an integer?

    Hello,

    I need to know whether the result of a division is a integer or not.
    dim x =60
    dim y = 3

    if x/y is a integer then
    ....
    else
    ....
    end if

    How can i know that?
    Thanks
    André


  • Theo Verweij

    #2
    Re: how to check whether a number is an integer?

    If x/y = x\y then
    'Is an integer
    else
    'is not
    end if

    André wrote:
    Hello,

    I need to know whether the result of a division is a integer or not.
    dim x =60
    dim y = 3

    if x/y is a integer then
    ....
    else
    ....
    end if

    How can i know that?
    Thanks
    André

    Comment

    • André

      #3
      Re: how to check whether a number is an integer?

      Thanks

      "Theo Verweij" <tverweij@xs4al l.nlschreef in bericht
      news:%231XBiNe% 23GHA.4376@TK2M SFTNGP03.phx.gb l...
      If x/y = x\y then
      'Is an integer
      else
      'is not
      end if

      André wrote:
      Hello,
      >
      I need to know whether the result of a division is a integer or not.
      dim x =60
      dim y = 3
      >
      if x/y is a integer then
      ....
      else
      ....
      end if
      >
      How can i know that?
      Thanks
      André
      >

      Comment

      • Mythran

        #4
        Re: how to check whether a number is an integer?



        "André" <svs@sdv.sdwrot e in message
        news:eahb7ge#GH A.4464@TK2MSFTN GP02.phx.gbl...
        Thanks
        >
        "Theo Verweij" <tverweij@xs4al l.nlschreef in bericht
        news:%231XBiNe% 23GHA.4376@TK2M SFTNGP03.phx.gb l...
        If x/y = x\y then
        'Is an integer
        else
        'is not
        end if
        >
        André wrote:
        >Hello,
        >>
        >I need to know whether the result of a division is a integer or not.
        >dim x =60
        >dim y = 3
        >>
        >if x/y is a integer then
        >....
        >else
        >....
        >end if
        >>
        >How can i know that?
        >Thanks
        >André
        >>
        >
        >
        I would actually use Mod (modulus) to determine if a division result
        (remainder) is 0.

        Example:

        Dim x As Integer = 12
        Dim y As Integer = 6
        Dim isInt As Boolean = x Mod y = 0

        Console.WriteLi ne( _
        "Remainder of {0} / {1} = {2} : Integral = {3}", _
        x.ToString(), _
        y.ToString(), _
        (x / y).ToString(), _
        (x Mod y = 0).ToString() _
        )

        The last parameter (x Mod y = 0) is the part that I would use :) So:

        If x Mod y = 0 Then
        ' x / y results in a 0 remainder, meaning an int result (given that the
        result falls within the scope of an integer and not a long.
        Else
        ' x / y results in a non-0 remainder, meaning a floating point result.
        End If

        HTH :)

        Mythran


        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: how to check whether a number is an integer?

          "André" <svs@sdv.sdschr ieb:
          I need to know whether the result of a division is a integer or not.
          dim x =60
          dim y = 3
          >
          if x/y is a integer then
          ...
          else
          ...
          end if
          'If Int(x) = x Then...'

          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

          Comment

          • Cor Ligthert [MVP]

            #6
            Re: how to check whether a number is an integer?

            Andre,

            Put option strict on in top of your program, than it will divide only
            doubles using the non integer divide.

            Cor

            "André" <svs@sdv.sdschr eef in bericht
            news:Ou8oULe%23 GHA.1172@TK2MSF TNGP03.phx.gbl. ..
            Hello,
            >
            I need to know whether the result of a division is a integer or not.
            dim x =60
            dim y = 3
            >
            if x/y is a integer then
            ...
            else
            ...
            end if
            >
            How can i know that?
            Thanks
            André
            >

            Comment

            Working...