another, (kilo str i)
; if exponent
_$ (kilo (rtos -98765432101234567890.123456789 2 3) 15)
"-9.877E+19" ?
; supply just string
_$ (kilo "-98765432101234567890.123456789" 15)
"-98,765,432,101,234,567,890.123456789000000"
;http://www.theswamp.org/index.php?topic=47391.0
;hp#010 13/07/2014
(defun kilo ($ i / l lst dot len str add); argument: $=string 'STR & i=precision 'INT
(setq l (vl-string->list $))
(if (or(member 69 l)(member 101 l)) $
(setq dot (member 46 l) len (length dot)
lst (if(cdr (member 45 l))(reverse (cdr l))(reverse l))
add '((i z / l) (setq l (if z (list z)'())) (repeat i (setq l (append l (list 48)))))
str (append (reverse (cdr (reverse (apply 'append
(reverse (mapcar '(lambda (x) (append (reverse x) '(44)))
(split (if dot(cdr (member 46 lst))lst)3 nil))))))) ;_ end of reverse
(cond ((= i 0) (car (split dot (1+ i) nil)))
((= len 0) (append dot (add i 46)))
((= len 1) (append dot (add (1- i) 48)))
((= len i) (append dot (add 1 nil)))
((< len i) (append dot (add (- i len) 48)))
((> len i) (car (split dot (1+ i) nil)))
(T dot)
) ;_ end of cond
);_ end of append
$ (strcat (if (member 45 l)"-" "")(vl-list->string str))
) ;_ end of setq
) ;_ end of if
) ;_ end of defun
;test:
;(kilo (rtos -9876543210.123456789 2 3) 5)
;"-9,876,543,210.12300"
;usage: add comma separated of thousands on decimal string. (not a rtos function)
;rtos to round
;lambda not optimized
;hanhphuc 2014
;sub function: split
;http://www.cadtutor.net/forum/showthread.php?87320-Break-a-list-in-two-sub-lists&highlight=Split+list
(defun split (lst len opt / ls l i) ; opt, T= by division or nil=by length
(setq i 1 l '() len (if opt (/ (length lst) len) len))
(while lst
(setq l (append l (list(car lst))))
(if
(zerop (rem i len))
(setq ls (cons l ls) l nil)
)
(setq i (1+ i) lst (cdr lst))
) ;_ end of foreach
(if l
(append (reverse ls) (list l))
(reverse ls)
) ;_ end of if
) ;_ end of defun
EDIT: zero unused, member 101, link added