+ Reply to Thread
Results 1 to 3 of 3

Should i convert string to number before comparing against a number or leave as is

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,666

    Should i convert string to number before comparing against a number or leave as is

    Hi,

    I have a question and would like a recommendation:

    I have a situation where i have string "tempstring" and

    Please Login or Register  to view this content.
    if i do the following conditional

    Please Login or Register  to view this content.
    it seems to work......and i dont know why? if i change assignment to tempstring = "0" then it still works. Dont know why.

    Also, i was thinking that it would not work and that i should convert the string to a number first but if it works should i?

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Should i convert string to number before comparing against a number or leave as is

    No necessarily!
    If you are comparing the same data type-string with a string or a number with a number, you do not need to convert one to another. However, if one cell is formatted or declared as a string, another one as a different data, they will not match.

  3. #3
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,858

    Re: Should i convert string to number before comparing against a number or leave as is

    A lot depends on what "greater than" and "less than" mean to you.

    As text strings: the text string "2" is greater than the text string "10"
    As numbers: the number 2 is less than the number 10.

    The examples you give just happen to be the same whether as text string or as number, but there are definitely examples where they are not. You as the programmer will need to understand how number sort order and text sort order are different and will need to decide which is more correct for you situation, or if it really matters at all.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] convert string to number
    By pannam in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-11-2014, 05:25 AM
  2. Convert string to number
    By djbomaha in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-27-2012, 04:40 PM
  3. Replies: 1
    Last Post: 01-25-2010, 12:48 PM
  4. [SOLVED] Convert a number to a name string
    By Numbers to name string in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-23-2006, 11:55 AM
  5. Convert a String to an number
    By 11Oppidan in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-02-2005, 12:06 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1