teknomatika Posted September 2, 2010 Posted September 2, 2010 (edited) Hi I needed your help: I needed an AutoLISP routine that allows a select group of integers numbers and convert them into real numbers, entering its decimal separator. For example: to convert 23678 into 23.678 or 23678 into 236.78 It would be important to determine the position of the decimal separator. Edited September 2, 2010 by teknomatika Quote
lpseifert Posted September 2, 2010 Posted September 2, 2010 (edited) $ (/ 23678 1000.) 23.678 _$ (/ 23678 100.) 236.78 _$ That was too easy... Are the numbers you want to convert Integers or Strings? If you expect to select them from the dwg they are most likely to be strings. Edited September 2, 2010 by lpseifert 1 Quote
teknomatika Posted September 3, 2010 Author Posted September 3, 2010 $ (/ 23678 1000.) 23.678 _$ (/ 23678 100.) 236.78 _$ That was too easy... Are the numbers you want to convert Integers or Strings? If you expect to select them from the dwg they are most likely to be strings. Ok, maybe I have not explained the best way: The integers may be strings. The important thing is that the conversion to real numbers, with option to determine the position of the decimal separator, can be accomplished simultaneously and an unlimited selection of these entities. The numbers that I stated were indicative. Obviously, many more and may be selected simultaneously. Sorry for my poor English. Tanks. Quote
vins1987 Posted September 3, 2010 Posted September 3, 2010 (edited) hi teknomatika, This is my first Post in this forum. I understand that you want to convert the integer nos in text or mtext to real nos. Otherwise please let me know. Check this code. (defun c:cint() (command "undo" "be") (vl-load-com) (setq cmd (getvar 'cmdecho)) (command "cmdecho" "0") (initget 3) (setq dsep(getreal "\nEnter the Decimal Separator (10, 100, 1000.....):") ss(ssget '((0 . "text,Mtext"))) len(sslength ss) i 0 ) (repeat len (progn (setq en(entget (ssname ss i)) tx(assoc 1 en) int(atoi (cdr tx)) ) (if (and (/= int nil) (/= int 0)) (progn (setq real_no(VL-STRING-RIGHT-TRIM "0" (rtos (/ int dsep) 2 ) newtx(cons 1 real_no) ) (setq en(subst newtx tx en)) (entmod en) ) ) (setq i (1+ i)) ) ) (command "cmdecho" cmd "undo" "e") (princ) ) Edited September 3, 2010 by vins1987 Quote
fixo Posted September 3, 2010 Posted September 3, 2010 My 2 cents ;; Convert To Decimal (defun ctd (str_num num_int) ;; str_num - source string (numerics only) ;; num_int - number of integers (must be positive, zero or negative number) (/ (atof str_num) (expt 10 (* 1.0 (- (strlen str_num) num_int) ) ) ) ) ;;usage: (setq result (ctd "123456789" 7)) (alert (strcat "Result = " (rtos result))) ~'J'~ Quote
teknomatika Posted September 3, 2010 Author Posted September 3, 2010 (edited) [color=#000000]vins1987: Hi, great job. [/color]Works correctly. That's exactly what wanted. Tanks! By the way, I show that though it was trying to do, and who also now works. But just remove the zeros, which does not want that to happen. By the way can help to correct my version? ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;copyright: teknomatika ;; (defun chgterr (s) (if (/= s "Function cancelled") (princ (strcat "\nError: " s)) ) (setq p nil) (setq *error* olderr) (princ) ) (defun C:r2int (/ s p div l n p s mn mn2 e chf chm olderr) (setq olderr *error* *error* chgterr chm 0) (setq p (ssget)) (setq div (getreal "\nChoice the number divisor factor - 10,100,100,1000...:")) (setq l 0 n (sslength p)) (while (< l n) (if (= "TEXT" (cdr (assoc 0 (setq e (entget (ssname p l)))))) (progn (setq s (cdr (setq as (assoc 1 e)))) (setq mn (atof s)) (setq mn2 (/ mn div)) (setq nm (+ mn div)) (setq s (rtos mn2)) (setq e (subst (cons 1 s) as e)) (entmod e) (setq chm (1+ chm)) ) ) (setq l (1+ l)) ) (princ chm) (princ " entidades de texto numérico") (princ " alteradas.") (terpri) (setq *error* olderr) (princ) ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Edited September 3, 2010 by teknomatika Quote
Tharwat Posted September 3, 2010 Posted September 3, 2010 Hello teknomatika. Read the following link to know how to post your codes in the forum which is better for you and for all to read. http://www.cadtutor.net/forum/showthread.php?9184-Code-posting-guidelines Thanks. Tharwat Quote
teknomatika Posted September 3, 2010 Author Posted September 3, 2010 tharwat316: Sorry, but I'm beginner. Already correct. Quote
Tharwat Posted September 3, 2010 Posted September 3, 2010 tharwat316: Sorry, but I'm beginner. Already correct. Thanks for your cooperation. But also you should read my username well. Thanks Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.