Jump to content

Convert integers numbers into real numbers, introducing decimal separator.


Recommended Posts

Posted (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 by teknomatika
Posted (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 by lpseifert
  • Thanks 1
Posted
$ (/ 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.

Posted (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 by vins1987
Posted

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'~

Posted (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 by teknomatika
Posted

tharwat316:

Sorry, but I'm beginner.

Already correct.

Posted
tharwat316:

Sorry, but I'm beginner.

Already correct.

 

Thanks for your cooperation.

 

But also you should read my username well. :)

 

Thanks

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...