Consulting

Results 1 to 7 of 7

Thread: Integer & String

  1. #1

    Integer & String

    I have this cell formated percentage. If I were to enter 10, the cell will give me 10% and if I were to enter .1, the cell will also give me 10%.

    I'm writing some codes that need to use this cell. Should I dim this cell as string or integer?

    Mathmatically, I know 0.1 is not an integer.

    Please shed some lights on this.

    Thanks

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    No, you should dim it as Double.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Quote Originally Posted by xld
    No, you should dim it as Double.
    Thanks for replying, but would you mind to explain why Double?

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Because it has a decimal portion and double will handle that, integer will not.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Quote Originally Posted by xld
    Because it has a decimal portion and double will handle that, integer will not.
    Thanks a lot. What about String? Can string handle it?

  6. #6
    You should cast the variable to the data type that most represents its most used state.

    For example, if you're doing mostly numeric operations w this particular variable, then cast it as Double, and if/when the time comes to represent it as a string, use Str() or Format() to do the conversion.

    Likewise, if you cast is as String, use CDbl to convert it to a Double.

  7. #7
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Cells contain only data types Double, String, Boolean, or Error.

    When reading from a cell, I always use Variant data type. I never trust the user to put the right data in the cell.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •