# Thread: Calculating the sum of selected numbers

1. ## Calculating the sum of selected numbers

Registered forum members do not see this ad.

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.

Code:
```;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```

2. Give this a try:

Code:
```(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

3. Thanks a lot Lee, it works perfectly. As usual, you are the man.

4. Cheers Rod, you're very welcome

5. Whilst i was trying to figure out what to do with the "," thingy. Lee came and went.
**** Gone in 60 seconds ****

Nice code btw Lee

6. Registered forum members do not see this ad.

Cheers pBe

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts