convert String to real?

Discussion in 'AutoCAD' started by Virgis, Aug 3, 2004.

  1. Virgis

    Virgis Guest

    Hi,
    What Vlisp command would convert String to real?



    (defun c:AOF ()
    (SETQ GL (ENTGET (ENTLAST)))
    (SETQ GT (CDR (ASSOC 1 A)))
    (setq GA (* 0.0001 GT))
    (print GA)
    (print)
    )

    error: bad argument type: consp "1998289.8296"
     
    Virgis, Aug 3, 2004
    #1
  2. Virgis

    Jürg Menzi Guest

    Hi Virgis

    'atof' like Alan says. To prevent unexpected errors, I would propose this
    function:
    (defun MeAtof (Val)
    (if (not (wcmatch Val "*[~0-9.-]*")) (atof Val))
    )
    I've seen, that you plan to read a text string. Probably this string can
    contain characters also. In this case 'atof' returns 0, 'MeAtof' returns nil.
    Following this, your function may work like this:
    ....
    (if (setq RetVal (MeAtof GT))
    (setq GA (* 0.0001 RetVal))
    (setq GA "Text is not nummeric.")
    )
    (print GA)

    Cheers
     
    Jürg Menzi, Aug 3, 2004
    #2
  3. Virgis

    Jim Claypool Guest

    (COMMAND "TEXT" PAUSE "" (rtos BB 2 2))
     
    Jim Claypool, Aug 3, 2004
    #3
  4. Virgis

    Virgis Guest

    Thanks it works!

    (defun c:AOF ()
    (SETQ A (ENTGET (ENTLAST)))
    (SETQ AA (CDR (ASSOC 1 A)))
    (SETQ B (ATOF AA))
    (setq BB (* 0.0001 B))
    (COMMAND "TEXT" PAUSE "" BB)
    )

    But is still one problem
    I become such result

    187.7116600000000

    I need this one:

    187.71

    Virgis
     
    Virgis, Aug 3, 2004
    #4
  5. Virgis

    Virgis Guest

    This is good one :)


    (defun c:TU ()
    (SETQ SS (CAR (ENTSEL)))
    (SETQ A (ENTGET SS))
    (SETQ AA (CDR (ASSOC 1 A)))
    (SETQ B (ATOF AA))
    (setq BB (* 0.0001 B))
    (setq C (RTOS BB 2 2))
    (SETQ CC (vl-string-right-trim "0" C))
    (COMMAND "TEXT" "J" "BR" PAUSE "0" CC)
    )
     
    Virgis, Aug 3, 2004
    #5
  6. Virgis

    Jim Claypool Guest

    If you trim the trailing 0's and your value is 187.00 you will have "187."
    Is this really what you want? You might want to trim the decimal point too.

    (SETQ CC (vl-string-trim "." (vl-string-right-trim "0" C)))
     
    Jim Claypool, Aug 3, 2004
    #6
  7. Virgis

    Virgis Guest

    Actually I want to have the result 187.00
    and now I have 187
    Maybe You know how?




    ----- Original Message -----
    From: "Jim Claypool" <jclaypool(removethis)@kc.rr.com>
    Newsgroups: autodesk.autocad.customization
    Sent: Tuesday, August 03, 2004 8:41 PM
    Subject: Re: convert String to real?
     
    Virgis, Aug 4, 2004
    #7
  8. Virgis

    MP Guest

    Command: (rtos (atof "187") 2 2)
    "187.00"
     
    MP, Aug 4, 2004
    #8
  9. Virgis

    Jim Claypool Guest

    (defun c:TU ()
    (SETQ SS (CAR (ENTSEL)))
    (SETQ A (ENTGET SS))
    (SETQ AA (CDR (ASSOC 1 A)))
    (SETQ B (ATOF AA))
    (setq BB (* 0.0001 B))
    (setq C (RTOS BB 2 2))
    ;; (SETQ CC (vl-string-right-trim "0" C)) ;; REMOVE THIS LINE
    (COMMAND "TEXT" "J" "BR" PAUSE "0" C) ;; CHANGE CC to C
    )
     
    Jim Claypool, Aug 5, 2004
    #9
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.