Author Topic: Adding thousand dividing comma to result values of 4 Arithmetic Lisp  (Read 3732 times)

0 Members and 1 Guest are viewing this topic.

asami486

  • Guest
I have a lisp to calculate by 4 arithmetic.
This lisp works for +, - , / , *.
But the result value by this lisp doesn't have a thousand dividing comma.
For example, If I command "+(add)" to add "1,000" to "2,000",
It shows me "3000" that doesn't have a thousand dividing comma.
It also doesn't in cases of - , / , *.
Could the lisp be modified to add a thousand dividing comma to result values?!
I attach the lisp file.

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Here are some functions to allow you to convert a number to a string with thousands separators - simply evaluate the function in place of rtos.

E.g.:
Code - Auto/Visual Lisp: [Select]
  1. _$ (rtoc 12345.678 3)
  2. "12,345.678"

Grrr1337

  • Swamp Rat
  • Posts: 812
Lee that looks insane!
BTW I thought about using vl-string->list with:
Code - Auto/Visual Lisp: [Select]
  1. ;; Group by Number  -  Lee Mac
  2. ;; Groups a list 'l' into a list of lists, each of length 'n'
  3.  
  4. (defun LM:group-n ( l n / r )
  5.     (if l
  6.         (cons
  7.             (reverse (repeat n (setq r (cons (car l) r) l (cdr l)) r))
  8.             (LM:group-n l n)
  9.         )
  10.     )
  11. )
and with a little list manipulation could be achieved the desired result.  :-)
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

asami486

  • Guest
Thanks LEE, GRRRR!  :-)
But I'm afraid that I don't know where to add that function in my lisp.
Could you make a lisp file added that function in base of my lisp file if you don't mind???

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Lee that looks insane!

Thanks  :-)

BTW I thought about using vl-string->list with:
Code: [Select]
< ... >and with a little list manipulation could be achieved the desired result.  :-)

Indeed - there are likely many ways to obtain the result  :-)

Thanks LEE, GRRRR!  :-)
But I'm afraid that I don't know where to add that function in my lisp.
Could you make a lisp file added that function in base of my lisp file if you don't mind???

First, copy the code for the function on a new line at the end of your file.

Then, replace every occurrence of  (rtos <variable> 2 3) [e.g.  (rtos sum_m 2 3)] with (rtoc <variable> 3) [e.g.  (rtoc sum_m 3)]

Let us know if you get stuck!

asami486

  • Guest
Code: [Select]
;;program name : 캐드내 사칙연산프로그램 ver 1.0
;;program : +, -, /, * 가지고 텍스트 계산 다 됨
;;Modified by KDUB (in theswamp)
;;https://www.theswamp.org/index.php?topic=52639.0


(defun exe_1()
   (setvar "cmdecho" 0)
   (setvar "blipmode" 0)
   (setq e1 (entsel "\n>>첫번째숫자선택:"))
   (setq a (car e1);;----------엔티티의 이름값
         b (entget a);;--------엔티티의 리스트값
         txt1 (assoc 1 b);;----엔티티중 해당 문자열 리스트
         tt1 (cdr txt1);;------엔티티 문자열의 리스트중 뒤의값="문자열"
         sum1 (atof(_removecommas-fromstring tt1));;-----엔티티의 문자열을 정수로 변환=100
         txth1 (assoc 40 b);;--엔티티 리스트중 텍스트 높이의 리스트값(40,100)
         thh (cdr txth1);;-----엔티티의 텍스트높이의 리스트중 뒤의값=100
   )         
   (setq e2 (entsel "\n>>두번째숫자선택:"))
   (setq aa (car e2)
         bb (entget aa)
         txt2 (assoc 1 bb)
         tt2 (cdr txt2)
         sum2 (atof(_removecommas-fromstring tt2))
         )
    (setq tl (assoc 8 b)
          txtst (cdr tl)
    )
)

(defun exe+()
   (setq sum (+ sum1 sum2))
   (setq summ (rtos sum 2 3))
   (setq p1 (getpoint "\n>>표시할 포인트찍기:"))
   (command "text" p1 thh 0 summ)
     
 )
(defun exe-()
   (setq sum (- sum1 sum2))
   (setq summ (rtos sum 2 3))
   (setq p1 (getpoint "\n>>표시할 포인트찍기:"))
   (command "text" p1 thh 0 summ)
 
)

 
 (defun exe*()
    (setq sum (* sum1 sum2))
    (setq summ (rtos sum 2 3))
    (setq p1 (getpoint "\n>>표시할 포인트찍기:"))
    (command "text" p1 thh 0 summ)
 )

 (defun exe/()
    (setq sum (/ sum1 sum2))
    (setq summ (rtos sum 2 3))
    (setq p1 (getpoint "\n>>표시할 포인트찍기:"))
    (command "text" p1 thh 0 summ)
 )
 (defun exe%()
    (setq sum (* (/ sum1 sum2) 100))
    (setq summ (rtos sum 2 3))
    (setq p1 (getpoint "\n>>표시할 포인트찍기:"))
    (command "text" p1 thh 0 summ)
 )

(defun c:+( / e1 a b txt1 tt1 sum1 txth1 thh e2 aa bb txt2 tt2 sum2 tl txtst sum summ p1)
  (exe_1)
  (exe+)
  (princ)
)
(defun c:-( / e1 a b txt1 tt1 sum1 txth1 thh e2 aa bb txt2 tt2 sum2 tl txtst sum summ p1)
   (exe_1)
   (exe-)
   (princ)
)
(defun c:*( / e1 a b txt1 tt1 sum1 txth1 thh e2 aa bb txt2 tt2 sum2 tl txtst sum summ p1)
   (exe_1)
   (exe*)
   (princ)
)
(defun c:/( / e1 a b txt1 tt1 sum1 txth1 thh e2 aa bb txt2 tt2 sum2 tl txtst sum summ p1)
  (exe_1)
  (exe/)
  (princ)
)
(defun c:%( / e1 a b txt1 tt1 sum1 txth1 thh e2 aa bb txt2 tt2 sum2 tl txtst sum summ p1)
  (exe_1)
  (exe%)
  (princ)
)
(defun c:py( / e1 a b txt1 tt1 sum1 txth1 thh sum summ p1 )
   (setvar "cmdecho" 0)
   (setvar "blipmode" 0)
   (setq e1 (entsel "\n>>숫자선택:"))
   (setq a (car e1);;----------엔티티의 이름값
         b (entget a);;--------엔티티의 리스트값
         txt1 (assoc 1 b);;----엔티티중 해당 문자열 리스트
         tt1 (cdr txt1);;------엔티티 문자열의 리스트중 뒤의값="문자열"
         sum1 (atof(_removecommas-fromstring tt1));;-----엔티티의 문자열을 정수로 변환=100
         txth1 (assoc 40 b);;--엔티티 리스트중 텍스트 높이의 리스트값(40,100)
         thh (cdr txth1);;-----엔티티의 텍스트높이의 리스트중 뒤의값=100
   )
    (setq sum (* sum1 0.3025))
    (setq summ (rtos sum 2 3))
    (setq zz (strcat "(" summ ")"))
    (setq p1 (getpoint "\n>>표시할 포인트찍기:"))
    (command "text" p1 thh 0 zz)
(princ)
)

(defun c:+( / sum_m ss ok e ent sum_ p1 txth1 thh summ )
 (setvar "cmdecho" 0)
 (setvar "blipmode" 0)
 (setq sum_m 0)
  (setq SS (ssget))
  (setq ok 0)
  (while
    (setq e (ssname ss ok))
    (setq ent (entget e))
    (setq sum_ (assoc 1 ent))
    (setq sum_ (cdr sum_))
    (setq sum_ (atof(_removecommas-fromstring sum_)))
    (setq sum_m (+ sum_m sum_))
    (setq ok (1+ ok))
  )
 (setq p1 (getpoint "\n>>표시할 포인트찍기:"))
    (setq txth1 (assoc 40 ent))
    (setq thh (cdr txth1))
    (setq summ (rtos sum_m 2 3))
    (command "text" p1 thh 0 summ)
(princ)       
)

(defun c:*( / sum_m ss ok e ent sum_ p1 txth1 thh summ )
 (setvar "cmdecho" 0)
 (setvar "blipmode" 0)
 (setq sum_m 1)
  (setq SS (ssget))
  (setq ok 0)
  (while
    (setq e (ssname ss ok))
    (setq ent (entget e))
    (setq sum_ (assoc 1 ent))
    (setq sum_ (cdr sum_))
    (setq sum_ (atof(_removecommas-fromstring sum_)))
    (setq sum_m (* sum_m sum_))
    (setq ok (1+ ok))
  )
 (setq p1 (getpoint "\n>>표시할 포인트찍기:"))
    (setq txth1 (assoc 40 ent))
    (setq thh (cdr txth1))
    (setq summ (rtos sum_m 2 3))
    (command "text" p1 thh 0 summ)
(princ)       
)
(princ "\n>>사칙연산 프로그램 로딩완료 command : +,-,*,/,%,py,++,**")
(princ)

(defun _removecommas-fromstring (str)
  (while (vl-string-search "," str) (setq str (vl-string-subst "" "," str)))
   str
)



Mr. Lee.
I changed All "rtos sum_m 2 3" to "rtos sum_m 3" in that code.
But It doesn't work.
I got <453'-4"> as a result when I add <3,340> to <15,930>.

HasanCAD

  • Swamp Rat
  • Posts: 1421
rtoS ----> rtoC

Code - Auto/Visual Lisp: [Select]
  1. ;;program name : ijµå³» »çÄ¢¿¬»êÇÁ·Î±×·¥ ver 1.0
  2. ;;program : +, -, /, * °¡Áö°í ÅؽºÆ® °è»ê ´Ù µÊ
  3. ;;Modified by KDUB (in theswamp)
  4. ;;https://www.theswamp.org/index.php?topic=52639.0
  5.  
  6.  
  7. (defun rtoc ( n p / d i l x )
  8.     (setq d (getvar 'dimzin))
  9.     (setvar 'dimzin 0)
  10.     (setq l (vl-string->list (rtos (abs n) 2 p))
  11.           x (cond ((cdr (member 46 (reverse l)))) ((reverse l)))
  12.           i 0
  13.     )
  14.     (setvar 'dimzin d)
  15.     (vl-list->string
  16.         (append (if (minusp n) '(45))
  17.             (reverse
  18.                 (apply 'append
  19.                     (mapcar
  20.                        '(lambda ( a b )
  21.                             (if (and (zerop (rem (setq i (1+ i)) 3)) b)
  22.                                 (list a 44)
  23.                                 (list a)
  24.                             )
  25.                         )
  26.                         x (append (cdr x) '(nil))
  27.                     )
  28.                 )
  29.             )
  30.             (member 46 l)
  31.         )
  32.     )
  33. )
  34.  
  35. (defun exe_1()
  36.    (setvar "cmdecho" 0)
  37.    (setvar "blipmode" 0)
  38.    (setq e1 (entsel "\n>>ù¹ø°¼ýÀÚ¼±ÅÃ:"))
  39.    (setq a (car e1);;----------¿£Æ¼Æ¼ÀÇ À̸§°ª
  40.          b (entget a);;--------¿£Æ¼Æ¼ÀÇ ¸®½ºÆ®°ª
  41.          txt1 (assoc 1 b);;----¿£Æ¼Æ¼Áß ÇØ´ç ¹®ÀÚ¿­ ¸®½ºÆ®
  42.          tt1 (cdr txt1);;------¿£Æ¼Æ¼ ¹®ÀÚ¿­ÀÇ ¸®½ºÆ®Áß µÚÀÇ°ª="¹®ÀÚ¿­"
  43.          sum1 (atof(_removecommas-fromstring tt1));;-----¿£Æ¼Æ¼ÀÇ ¹®ÀÚ¿­À» Á¤¼ö·Î º¯È¯=100
  44.          txth1 (assoc 40 b);;--¿£Æ¼Æ¼ ¸®½ºÆ®Áß ÅؽºÆ® ³ôÀÌÀÇ ¸®½ºÆ®°ª(40,100)
  45.          thh (cdr txth1);;-----¿£Æ¼Æ¼ÀÇ ÅؽºÆ®³ôÀÌÀÇ ¸®½ºÆ®Áß µÚÀÇ°ª=100
  46.    )          
  47.    (setq e2 (entsel "\n>>µÎ¹ø°¼ýÀÚ¼±ÅÃ:"))
  48.    (setq aa (car e2)
  49.          bb (entget aa)
  50.          txt2 (assoc 1 bb)
  51.          tt2 (cdr txt2)
  52.          sum2 (atof(_removecommas-fromstring tt2))
  53.          )
  54.     (setq tl (assoc 8 b)
  55.           txtst (cdr tl)
  56.     )
  57. )
  58.  
  59. (defun exe+()
  60.    (setq sum (+ sum1 sum2))
  61.    (setq summ (rtoc sum 3))
  62.    (setq p1 (getpoint "\n>>Ç¥½ÃÇÒ Æ÷ÀÎÆ®Âï±â:"))
  63.    (command "text" p1 thh 0 summ)
  64.      
  65.  )
  66. (defun exe-()
  67.    (setq sum (- sum1 sum2))
  68.    (setq summ (rtoc sum 3))
  69.    (setq p1 (getpoint "\n>>Ç¥½ÃÇÒ Æ÷ÀÎÆ®Âï±â:"))
  70.    (command "text" p1 thh 0 summ)
  71.  
  72. )
  73.  
  74.  
  75.  (defun exe*()
  76.     (setq sum (* sum1 sum2))
  77.     (setq summ (rtoc sum 3))
  78.     (setq p1 (getpoint "\n>>Ç¥½ÃÇÒ Æ÷ÀÎÆ®Âï±â:"))
  79.     (command "text" p1 thh 0 summ)
  80.  )
  81.  
  82.  (defun exe/()
  83.     (setq sum (/ sum1 sum2))
  84.     (setq summ (rtoc sum 3))
  85.     (setq p1 (getpoint "\n>>Ç¥½ÃÇÒ Æ÷ÀÎÆ®Âï±â:"))
  86.     (command "text" p1 thh 0 summ)
  87.  )
  88.  (defun exe%()
  89.     (setq sum (* (/ sum1 sum2) 100))
  90.     (setq summ (rtoc sum 3))
  91.     (setq p1 (getpoint "\n>>Ç¥½ÃÇÒ Æ÷ÀÎÆ®Âï±â:"))
  92.     (command "text" p1 thh 0 summ)
  93.  )
  94.  
  95. (defun c:+( / e1 a b txt1 tt1 sum1 txth1 thh e2 aa bb txt2 tt2 sum2 tl txtst sum summ p1)
  96.   (exe_1)
  97.   (exe+)
  98.   (princ)
  99. )
  100. (defun c:-( / e1 a b txt1 tt1 sum1 txth1 thh e2 aa bb txt2 tt2 sum2 tl txtst sum summ p1)
  101.    (exe_1)
  102.    (exe-)
  103.    (princ)
  104. )
  105. (defun c:*( / e1 a b txt1 tt1 sum1 txth1 thh e2 aa bb txt2 tt2 sum2 tl txtst sum summ p1)
  106.    (exe_1)
  107.    (exe*)
  108.    (princ)
  109. )
  110. (defun c:/( / e1 a b txt1 tt1 sum1 txth1 thh e2 aa bb txt2 tt2 sum2 tl txtst sum summ p1)
  111.   (exe_1)
  112.   (exe/)
  113.   (princ)
  114. )
  115. (defun c:%( / e1 a b txt1 tt1 sum1 txth1 thh e2 aa bb txt2 tt2 sum2 tl txtst sum summ p1)
  116.   (exe_1)
  117.   (exe%)
  118.   (princ)
  119. )
  120. (defun c:py( / e1 a b txt1 tt1 sum1 txth1 thh sum summ p1 )
  121.    (setvar "cmdecho" 0)
  122.    (setvar "blipmode" 0)
  123.    (setq e1 (entsel "\n>>¼ýÀÚ¼±ÅÃ:"))
  124.    (setq a (car e1);;----------¿£Æ¼Æ¼ÀÇ À̸§°ª
  125.          b (entget a);;--------¿£Æ¼Æ¼ÀÇ ¸®½ºÆ®°ª
  126.          txt1 (assoc 1 b);;----¿£Æ¼Æ¼Áß ÇØ´ç ¹®ÀÚ¿­ ¸®½ºÆ®
  127.          tt1 (cdr txt1);;------¿£Æ¼Æ¼ ¹®ÀÚ¿­ÀÇ ¸®½ºÆ®Áß µÚÀÇ°ª="¹®ÀÚ¿­"
  128.          sum1 (atof(_removecommas-fromstring tt1));;-----¿£Æ¼Æ¼ÀÇ ¹®ÀÚ¿­À» Á¤¼ö·Î º¯È¯=100
  129.          txth1 (assoc 40 b);;--¿£Æ¼Æ¼ ¸®½ºÆ®Áß ÅؽºÆ® ³ôÀÌÀÇ ¸®½ºÆ®°ª(40,100)
  130.          thh (cdr txth1);;-----¿£Æ¼Æ¼ÀÇ ÅؽºÆ®³ôÀÌÀÇ ¸®½ºÆ®Áß µÚÀÇ°ª=100
  131.    )
  132.     (setq sum (* sum1 0.3025))
  133.     (setq summ (rtoc sum 3))
  134.     (setq zz (strcat "(" summ ")"))
  135.     (setq p1 (getpoint "\n>>Ç¥½ÃÇÒ Æ÷ÀÎÆ®Âï±â:"))
  136.     (command "text" p1 thh 0 zz)
  137. )
  138.  
  139. (defun c:+( / sum_m ss ok e ent sum_ p1 txth1 thh summ )
  140.  (setvar "cmdecho" 0)
  141.  (setvar "blipmode" 0)
  142.  (setq sum_m 0)
  143.   (setq SS (ssget))
  144.   (setq ok 0)
  145.   (while
  146.     (setq e (ssname ss ok))
  147.     (setq ent (entget e))
  148.     (setq sum_ (assoc 1 ent))
  149.     (setq sum_ (cdr sum_))
  150.     (setq sum_ (atof(_removecommas-fromstring sum_)))
  151.     (setq sum_m (+ sum_m sum_))
  152.     (setq ok (1+ ok))
  153.   )
  154.  (setq p1 (getpoint "\n>>Ç¥½ÃÇÒ Æ÷ÀÎÆ®Âï±â:"))
  155.     (setq txth1 (assoc 40 ent))
  156.     (setq thh (cdr txth1))
  157.     (setq summ (rtoc sum_m 3))
  158.     (command "text" p1 thh 0 summ)
  159. (princ)      
  160. )
  161.  
  162. (defun c:*( / sum_m ss ok e ent sum_ p1 txth1 thh summ )
  163.  (setvar "cmdecho" 0)
  164.  (setvar "blipmode" 0)
  165.  (setq sum_m 1)
  166.   (setq SS (ssget))
  167.   (setq ok 0)
  168.   (while
  169.     (setq e (ssname ss ok))
  170.     (setq ent (entget e))
  171.     (setq sum_ (assoc 1 ent))
  172.     (setq sum_ (cdr sum_))
  173.     (setq sum_ (atof(_removecommas-fromstring sum_)))
  174.     (setq sum_m (* sum_m sum_))
  175.     (setq ok (1+ ok))
  176.   )
  177.  (setq p1 (getpoint "\n>>Ç¥½ÃÇÒ Æ÷ÀÎÆ®Âï±â:"))
  178.     (setq txth1 (assoc 40 ent))
  179.     (setq thh (cdr txth1))
  180.     (setq summ (rtoc sum_m 3))
  181.     (command "text" p1 thh 0 summ)
  182. (princ)      
  183. )
  184. (princ "\n>>»çÄ¢¿¬»ê ÇÁ·Î±×·¥ ·Îµù¿Ï·á command : +,-,*,/,%,py,++,**")
  185.  
  186. (defun _removecommas-fromstring (str)
  187.   (while (vl-string-search "," str) (setq str (vl-string-subst "" "," str)))
  188.    str
  189. )
« Last Edit: March 04, 2017, 03:51:21 AM by HasanCAD »

asami486

  • Guest
Thanks Mr.Hasan.
It works for me.
But when I add "4,000" to "4,000", I got the result "8,000.000" with unnecessary decimal point(.000).
I'd like to get a decimal point only when each of numbers have decimal points.

eg. 4,000      + 4,000       = 8,000
      4,000.1   + 4,000.1    = 8,000.2
      4,000.01 + 4,000.01  = 8,000.02
            .
            .
            .
« Last Edit: March 04, 2017, 07:11:33 AM by asami486 »

ronjonp

  • Needs a day job
  • Posts: 7527
Thanks Mr.Hasan.
It works for me.
But when I add "4,000" to "4,000", I got the result "8,000.000" with unnecessary decimal point(.000).
I'd like to get a decimal point only when each of numbers have decimal points.

eg. 4,000      + 4,000       = 8,000
      4,000.1   + 4,000.1    = 8,000.2
      4,000.01 + 4,000.01  = 8,000.02
            .
            .
            .
If you're not going to calculate precision, you could do something like this:
Code - Auto/Visual Lisp: [Select]

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

asami486

  • Guest
Could you tell me where to add the text in my lisp?  :wideeyed:

w64bit

  • Newt
  • Posts: 78
Re: Adding thousand dividing comma to result values of 4 Arithmetic Lisp
« Reply #10 on: November 22, 2023, 12:59:44 PM »
Newbie question:
How can I have space as separator instead of comma in rtoc code?
Thank you

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Adding thousand dividing comma to result values of 4 Arithmetic Lisp
« Reply #11 on: November 22, 2023, 03:50:00 PM »
Change 44 to 32  :-)

w64bit

  • Newt
  • Posts: 78
Re: Adding thousand dividing comma to result values of 4 Arithmetic Lisp
« Reply #12 on: November 23, 2023, 02:38:17 AM »
"The answer to life, the universe, and everything" is no more 42, but 32.
So cool.
Thank you very much.

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Adding thousand dividing comma to result values of 4 Arithmetic Lisp
« Reply #13 on: November 23, 2023, 05:36:51 AM »
"The answer to life, the universe, and everything" is no more 42, but 32.

 :lol: