Author Topic: Display numbers  (Read 4677 times)

0 Members and 1 Guest are viewing this topic.

MeasureUp

  • Bull Frog
  • Posts: 465
Display numbers
« on: July 11, 2014, 02:42:00 AM »
I know that "rtos" can setup the number presentation but just wonder if there is a way to show a number of 123456789.123 to 123,456,789.123

Thanks in advance.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Display numbers
« Reply #1 on: July 11, 2014, 04:14:51 AM »
Not a straight-forward one. Though you could roll your own like this:
Code - Auto/Visual Lisp: [Select]
  1. (defun rtos-1000group (number precision / portions frac minus)
  2.   (setq minus (minusp number)
  3.         number (abs number)
  4.         frac (rem number 1.0)
  5.         number (- number frac))
  6.   (while (> number 0.0)
  7.     (setq portions (cons (fix (rem number 1000.0)) portions)
  8.           number (/ (- number (car portions)) 1000.0)))
  9.   (strcat (if minus "-" "")
  10.           (apply 'strcat (cdr (apply 'append (mapcar (function (lambda (i) (list "," (itoa i)))) portions))))
  11.           (substr (rtos frac 2 precision) 2)))
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Display numbers
« Reply #2 on: July 11, 2014, 08:20:42 AM »
Another from my old files.
Code - Auto/Visual Lisp: [Select]
  1. ;;  By CAB  07/21/2006
  2. ;; (addComma "10987654321.0123")
  3. ;; "10,987,654,321.0123"
  4. (defun addcomma (txt / strl next newl idx)
  5.   (setq strl (reverse (vl-string->list txt)))
  6.   (if (vl-position 46 strl)
  7.     (while
  8.       (progn
  9.         (setq newl (cons (setq next (car strl)) newl)
  10.               strl (cdr strl))
  11.         (/= next 46)
  12.       )
  13.     )
  14.   )
  15.   (setq idx -1)
  16.   (while (setq next (car strl))
  17.     (if (= (setq idx (1+ idx)) 3)
  18.       (setq newl (cons 44 newl)
  19.             idx  0)
  20.     )
  21.     (setq newl (cons next newl)
  22.           strl (cdr strl))
  23.   )
  24.   (vl-list->string newl)
  25. )
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

PKENEWELL

  • Bull Frog
  • Posts: 320
Re: Display numbers
« Reply #3 on: July 11, 2014, 11:25:55 AM »
Not a straight-forward one. Though you could roll your own like this:
Code - Auto/Visual Lisp: [Select]
  1. (defun rtos-1000group (number precision / portions frac minus)
  2.   (setq minus (minusp number)
  3.         number (abs number)
  4.         frac (rem number 1.0)
  5.         number (- number frac))
  6.   (while (> number 0.0)
  7.     (setq portions (cons (fix (rem number 1000.0)) portions)
  8.           number (/ (- number (car portions)) 1000.0)))
  9.   (strcat (if minus "-" "")
  10.           (apply 'strcat (cdr (apply 'append (mapcar (function (lambda (i) (list "," (itoa i)))) portions))))
  11.           (substr (rtos frac 2 precision) 2)))

Hi Irné

Here is a modification of your code to fix a couple problems: 1) if the 1000 group starts with a "0" such as "36,012.0", the zero is stripped by the (itoa) function returning an incorrect result ("36,12.0"), and 2) you forgot to add the decimal back in to the string.

Code - Auto/Visual Lisp: [Select]
  1. (defun rtos-1000group (number precision / portions frac minus)
  2.   (setq minus (minusp number)
  3.         number (abs number)
  4.         frac (rem number 1.0)
  5.         number (- number frac)
  6.   )
  7.   (while (> number 0.0)
  8.     (setq portions (cons (fix (rem number 1000.0)) portions)
  9.           number (/ (- number (car portions)) 1000.0)
  10.     )
  11.   )
  12.   (strcat (if minus "-" "")
  13.           (vl-string-left-trim "0"
  14.              (apply 'strcat (cdr (apply 'append (mapcar (function (lambda (i) (list "," (pjk-leadzeros 3 (itoa i))))) portions))))
  15.           )
  16.           "."
  17.           (substr (rtos frac 2 precision) 2)
  18.   )
  19. )
  20.  
  21. ;; Creates a consistent string length for a string representing an integer.
  22. (defun pjk-leadzeros (dig str)
  23.         (if (and dig str (> dig 0))
  24.                 (repeat (- dig (strlen str))(setq str (strcat "0" str)))
  25.         )
  26.         str
  27. )
  28.  

EDIT: You might also want to use Lee Mac's "Consistent Rtos" function (http://www.lee-mac.com/consistentrtos.html) in place of (rtos frac 2 precision), because the leading zero may have been suppressed by the DIMZIN system variable.
« Last Edit: July 11, 2014, 11:52:39 AM by PKENEWELL »
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England

LE3

  • Guest
Re: Display numbers
« Reply #5 on: July 11, 2014, 02:56:00 PM »

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Display numbers
« Reply #6 on: July 12, 2014, 04:56:53 AM »
Hi Irné

Here is a modification of your code to fix a couple problems: 1) if the 1000 group starts with a "0" such as "36,012.0", the zero is stripped by the (itoa) function returning an incorrect result ("36,12.0"), and 2) you forgot to add the decimal back in to the string.
Thanks! Yes, that was a quick and dirty idea to show the basic algorithm. I can think of many fixes which aren't covered in it - e.g. what about grouping per locale settings?
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

hanhphuc

  • Newt
  • Posts: 64
Re: Display numbers
« Reply #7 on: July 13, 2014, 02:59:40 AM »
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"

Code: [Select]
;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
« Last Edit: July 13, 2014, 06:06:31 AM by hanhphuc »
( apply 'equal "hp" "happy" "hạnh phúc" "ハッピー" "幸福" "행복" ) ; error: too many arguments

MeasureUp

  • Bull Frog
  • Posts: 465
Re: Display numbers
« Reply #8 on: July 15, 2014, 01:00:24 AM »
Thanks to everyone's input.
Cheers!