Jump to content

Lisp routine that can add an addition symbol to multiple text


asra

Recommended Posts

Hello,

 

Is there a Lisp routine that can add an addition symbol to multiple text. eg. have a list all in single text 5889.12, 5685.23, etc.

 

Results will be 5+889.12, 5+685.23, etc.

 

Thanks,

Link to comment
Share on other sites

6 hours ago, asra said:

Hello,

 

Is there a Lisp routine that can add an addition symbol to multiple text. eg. have a list all in single text 5889.12, 5685.23, etc.

 

Results will be 5+889.12, 5+685.23, etc.

 

Thanks,

 

What happens with 0.62 or 7.93 or 29.01 or 168.75?

Link to comment
Share on other sites

15 hours ago, asra said:

Hello,

 

Is there a Lisp routine that can add an addition symbol to multiple text. eg. have a list all in single text 5889.12, 5685.23, etc.

 

Results will be 5+889.12, 5+685.23, etc.

 

Thanks,

 

ymg's rtosa 

 

Link to comment
Share on other sites

4 hours ago, hanhphuc said:

 

ymg's rtosa 

 

did you click the link provided at previous post above ?

 

1 hour ago, asra said:

HELLO All,

maksud saya bukan untuk menjumlahkan angka,
tapi untuk menambahkan simbol "+" 😅

 

;; rtosta                     by ymg  September 2013                          ;
;;                                                                            ;
;; Arguments:   sta Real number to format as a Station                        ;
;;             unit 1 for Imperials,                                          ;
;;                  2 for Metrics.                                            ;
;;             prec Integer for number of decimals                            ;
;;                                                                            ;
;; Examples: (rtosta 0 1 0)  -> "0+00"   (rtosta 1328.325 1 2) -> "13+28.33"  ;
;;           (rtosta 0 2 0)  -> "0+000"  (rtosta 1328.325 2 2) -> "1+328.33"  ;
;;                                                                            ;
;; If sta is negative, format is as follow:                                   ;
;;                                       (rtosta -1328.325 1 2) -> "13-28.33" ;
;;                                       (rtosta -1328.325 2 2) -> "1-328.33" ;
;;                                                                            ;
 
(defun rtosta (sta unit prec / str a b )
    (setq str (rtos (abs sta) 2 prec))
    (while (< (strlen str) (if (= prec 0) (+ unit 2) (+ prec (+ unit 3))))
        (setq str (strcat "0" str))
    )
    (setq a (if (= prec 0) (- (strlen str) unit) (- (strlen str) prec (+ unit 1)))
          b (substr str 1 (- a 1))
          a (substr str a)
    )
    (strcat b (if (minusp sta) "-" "+") a)
)

it does what exactly you asked

5889.12, 5685.23

(rtosta 5889.12 2 2)

"5+889.12"

(rtosta 5685.23 2 2)

"5+685.23"

Link to comment
Share on other sites

1 hour ago, asra said:

Error: too few arguments

 

 

you must supply 3 arguments in the function

assume your input causes error

(rtosta 1234.56 )

(rtosta 1234.56 22 ) 

 

(rtosta 1234.56  2  2 ) ; must use space

 

Link to comment
Share on other sites

;; rtosta                     by ymg  September 2013                          ;
;;                                                                            ;
;; Arguments:   sta Real number to format as a Station                        ;
;;             unit 1 for Imperials,                                          ;
;;                  2 for Metrics.                                            ;
;;             prec Integer for number of decimals                            ;
;;                                                                            ;
;; Examples: (rtosta 0 1 0)  -> "0+00"   (rtosta 1328.325 1 2) -> "13+28.33"  ;
;;           (rtosta 0 2 0)  -> "0+000"  (rtosta 1328.325 2 2) -> "1+328.33"  ;
;;                                                                            ;
;; If sta is negative, format is as follow:                                   ;
;;                                       (rtosta -1328.325 1 2) -> "13-28.33" ;
;;                                       (rtosta -1328.325 2 2) -> "1-328.33" ;
;;                                                                            ;
 
(defun rtosta (sta unit prec / str a b ) (rtosta 0 2 0)
    (setq str (rtos (abs sta) 2 prec)) 
    (while (< (strlen str) (if (= prec 0) (+ unit 2) (+ prec (+ unit 3))))
        (setq str (strcat "0" str))
    )
    (setq a (if (= prec 0) (- (strlen str) unit) (- (strlen str) prec (+ unit 1)))
          b (substr str 1 (- a 1))
          a (substr str a)
    )
    (strcat b (if (minusp sta) "-" "+") a)

(princ)
 
)

 

like this? 

Link to comment
Share on other sites

On 12/24/2019 at 1:21 PM, asra said:

 

like this? 

 

Please DO NOT modify the function!!!

copy again click -> here!

That's why you got error!!

 

(defun rtosta (sta unit prec / str a b ) (rtosta 0 2 0) <-- remove this!!!

<snippet>

(princ) ;<-- why ??

)

 

it seems you have zero knowledge in LISP?

try this example:

(defun c:test ( / TEXT WORD NUMBER OBJECT )
  (vl-load-com)
  (while
  (setq TEXT (car (entsel "\nSelect TEXT.. ")))
  (setq OBJECT (vlax-ename->vla-object TEXT))
  (if
  (and
  (vlax-property-available-p OBJECT 'TextString)
  (setq NUMBER (read (vla-get-TextString OBJECT ))); atof
  (numberp NUMBER)
  )
  (vla-put-Textstring OBJECT (rtosta NUMBER 2 2))
  )(princ "\nNUMBER please!!")
  )
  )
  (princ)
)

 

for multiple texts

1.use ssget for selection

2.loop repeat 

many examples, just search & learn

 

 

Edited by hanhphuc
symbol 'OBJECT' localized
Link to comment
Share on other sites

(defun rtosta (sta unit prec / str a b )
    (setq str (rtos (abs sta) 2 prec))
    (while (< (strlen str) (if (= prec 0) (+ unit 2) (+ prec (+ unit 3))))
        (setq str (strcat "0" str))
    )
    (setq a (if (= prec 0) (- (strlen str) unit) (- (strlen str) prec (+ unit 1)))
          b (substr str 1 (- a 1))
          a (substr str a)
    )
    (strcat b (if (minusp sta) "-" "+") a)
)
(defun c:test ( / TEXT WORD NUMBER )
  (vl-load-com)
  (while
  (setq TEXT (car (entsel "\nSelect TEXT.. ")))
  (setq OBJECT (vlax-ename->vla-object TEXT))
  (if
  (and
  (vlax-property-available-p OBJECT 'TextString)
  (setq NUMBER (read (vla-get-TextString OBJECT ))); atof
  (numberp NUMBER)
  )
  (vla-put-Textstring OBJECT (rtosta NUMBER 2 0))
  )(princ "\nNUMBER please!!")
  )
  )
  (princ)
)

OK, its works 😁😁 thanks, so much.......

 

what if in selecting text not one by one, but at the same time a lot of text ?

Edited by asra
Link to comment
Share on other sites

(defun c:test ( / TEXT WORD NUMBER )
  (vl-load-com)
  (if (setq sset (ssget '((0 . "text"))))
    (repeat (setq cnt (sslength sset))
      (setq TEXT (ssname sset (setq cnt (1- cnt))))
      (setq OBJECT (vlax-ename->vla-object TEXT))
      (if
	(and
	  (vlax-property-available-p OBJECT 'TextString)
	  (setq NUMBER (read (vla-get-TextString OBJECT ))); atof
	  (numberp NUMBER)
	  )
	(vla-put-Textstring OBJECT (rtosta NUMBER 2 0))
	)(princ "\nNUMBER please!!")
      )
    )
  )
  (princ)
  
;; rtosta                     by ymg  September 2013                          ;
;;                                                                            ;
;; Arguments:   sta Real number to format as a Station                        ;
;;             unit 1 for Imperials,                                          ;
;;                  2 for Metrics.                                            ;
;;             prec Integer for number of decimals                            ;
;;                                                                            ;
;; Examples: (rtosta 0 1 0)  -> "0+00"   (rtosta 1328.325 1 2) -> "13+28.33"  ;
;;           (rtosta 0 2 0)  -> "0+000"  (rtosta 1328.325 2 2) -> "1+328.33"  ;
;;                                                                            ;
;; If sta is negative, format is as follow:                                   ;
;;                                       (rtosta -1328.325 1 2) -> "13-28.33" ;
;;                                       (rtosta -1328.325 2 2) -> "1-328.33" ;
;;                                                                            ;
 
(defun rtosta (sta unit prec / str a b )
    (setq str (rtos (abs sta) 2 prec))
    (while (< (strlen str) (if (= prec 0) (+ unit 2) (+ prec (+ unit 3))))
        (setq str (strcat "0" str))
    )
    (setq a (if (= prec 0) (- (strlen str) unit) (- (strlen str) prec (+ unit 1)))
          b (substr str 1 (- a 1))
          a (substr str a)
    )
    (strcat b (if (minusp sta) "-" "+") a)
)

 

Link to comment
Share on other sites

Just a different approach 5846.234 divide 1000 = 5.846234 the fix = 5  so add + then multiple decimal part by 1000 and add back on 5+846.234 obviously playing with numbers and strings.

Edited by BIGAL
Link to comment
Share on other sites

1 hour ago, BIGAL said:

Just a different approach 5846.234 divide 1000 = 5.846234 the fix = 5  so add + then multiple decimal part by 1000 and add back on 5+846.234 obviously playing with numbers and strings.

not quite..

Try 1.234

divide 1000 = 0.001234

fix 0 & back 0.001234 x1000 = 1.234

0+1.234

it should be

0+001.234

 

slightly tweak, but i ignore unit normally we use this format as decimal

(defun _rtosta (x prec / i n)
  (setq n (abs (/ x 1e3)) i (* 1e3 (- n (fix n))))
  (strcat (itoa (abs (fix n)))
    (if (minusp x) "-" "+")
    (cond
      ((< i 10.) "00") ((< i 100.) "0") (t "")
    )
          (rtos i 2 prec )
  )
)

(_rtosta 1.234 3)

"0+001.234"

 

wishing you a Merry X'mas 🎅

 

p/s: my company sucks! still a working day

 

Link to comment
Share on other sites

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...