Jump to content

Calculating the sum of selected numbers


Cad64

Recommended Posts

Hey guys, I'm looking for a routine that will add up the total of selected dtext numbers. These numbers are large square footage totals like 47,604.27 and 23,705.39.

 

I have the routine below, (that I got from the Cadalyst site), but it treats the commas like decimal points and rounds off to the nearest whole number. So when it adds the two amounts above it returns a total of 70.0 instead of 71,309.66.

 

If anyone has a routine, or can modify the routine below so that it can add large numbers with commas, and not round off the total, I will be forever grateful.

 

Thanks for your time. :)

 

;Tip1369A.LSP:   ADD.LSP   Add Selected Numbers   (P.K.Yousuf)

;Prepared by P.K.Yousuf 

(defun C:ADD (/ S1 LG INDEX A B C D)
 (setq S1    (ssget)
       LG    (sslength S1)
       INDEX 0
       C     0
 ) ;_ end of setq
 (while (/= INDEX LG)
   (setq A (entget (ssname S1 INDEX))
         D (cdr (assoc 0 A))
   ) ;_ end of setq
   (if (= D "TEXT")
     (progn
       (setq B (atof (cdr (assoc 1 A)))
             C (+ C B)
       ) ;_ end of setq
     ) ;_ end of progn
   ) ;_ end of if
   (setq INDEX (1+ INDEX))
 ) ;_ end of while
 (princ "\nTotal Value: ")
 (princ C)
 (princ)
)   ;end defun

Link to comment
Share on other sites

Give this a try:

 

(defun c:AddText ( / i r s x )
   (if (setq s (ssget '((0 . "TEXT") (1 . "*#*"))))
       (progn
           (setq r 0.0)
           (repeat (setq i (sslength s))
               (setq x (cdr (assoc 1 (entget (ssname s (setq i (1- i)))))))
               (while (wcmatch x "*`,*")
                   (setq x (vl-string-subst "" "," x))
               )
               (if (setq x (distof x))
                   (setq r (+ r x))
               )
           )
           (princ (strcat "\nTotal: " (rtos r)))
       )
   )
   (princ)
)

 

The printed result will be dependent on your settings of LUNITS / LUPREC

Link to comment
Share on other sites

Whilst i was trying to figure out what to do with the "," thingy. Lee came and went.

**** Gone in 60 seconds **** :lol:

Nice code btw Lee :thumbsup:

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