Is there any solution which can identify that a string is a number or character

Discussion in 'AutoCAD' started by homhsu, Sep 18, 2003.

  1. homhsu

    homhsu Guest

    Is there any solution which can identify that a string is a number or a
    character?
    for example:

    "89.56" <--> "get"
    how to tell the differecnces of that

    thanks
    homling
     
    homhsu, Sep 18, 2003
    #1
    1. Advertisements

  2. homhsu

    Ian A. White Guest

    (numberp) should tell you if it is a real or integer.

    --

    Regards,

    Ian A. White, CPEng

    WAI Engineering
    Sydney 2000
    Australia

    Ph: +61 418 203 229
    Fax: +61 2 9622 0450
    Home Page: www.wai.com.au
     
    Ian A. White, Sep 18, 2003
    #2
    1. Advertisements

  3. Command: (numberp (distof "get"))
    nil

    Command: (numberp (distof "89.56"))
    T
     
    Marc'Antonio Alessi, Sep 18, 2003
    #3
  4. homhsu

    Joe Burke Guest

    Herman,

    Just for fun. Does this work as well?

    ;; lacks data type check
    ;; returns nil or number
    (defun test_str2num (str)
    (if (= "." (substr str 1 1))
    (setq str (strcat "0" str)))
    (if (numberp (read str))
    (read str))
    )

    The reason for (strcat "0" str) is read chokes on (read ".01") with "misplaced
    dot on input".

    Joe Burke
     
    Joe Burke, Sep 18, 2003
    #4
  5. Joe,

    for more fun.....<g>

    (setq str ".01")

    (distof str)

    returns 0.01

    --
    Ken Alexander
    Acad2000
    Windows2000 Prof.

    "We can't solve problems by using the same kind
    of thinking we used when we created them."
    --Albert Einstein
     
    Ken Alexander, Sep 18, 2003
    #5
  6. homhsu

    DonB Guest

    Another possibility...

    Command: (setq str "fred")
    "fred"

    Command: (type (read str))
    SYM

    Command: (setq str "0.01")
    "0.01"

    Command: (type (read str))
    REAL

    Command: (setq str "1")
    "1"

    Command: (type (read str))
    INT

    Just compare the results of SYM REAL or INT

    Don
     
    DonB, Sep 18, 2003
    #6
  7. homhsu

    Joe Burke Guest

    Ken,

    Interesting. Should (read ".01") not error, or is it just being literal? As if
    we have any choice in the matter. :)

    Command: (distof "1")
    1.0
    Command: (str2num "1")
    1

    Joe Burke
     
    Joe Burke, Sep 18, 2003
    #7
  8. homhsu

    Joe Burke Guest

    Herman.

    Got it and thanks. I might rename it, "maybenum2num". :)

    Joe Burke
     
    Joe Burke, Sep 18, 2003
    #8
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.