Author Topic: Modifying function Tostring  (Read 1610 times)

0 Members and 1 Guest are viewing this topic.

Giuseppe Beatrice

  • Newt
  • Posts: 43
Modifying function Tostring
« on: November 17, 2018, 05:24:58 AM »
The Tostring function, prepared by MP in https://www.theswamp.org/index.php?topic=4814.0 has been very helpful for me in the preparation of lines of text for saving files containing lists with variable values to be recovered for later uses.
However, I found that saving real numbers of type 3.0 or 3. with a decimal part equal to 0 always resulted in a string "3"; this behavior is due to the dimzin system variable, which automatically suppresses the zeros, so I made a small change to the function itself.
Code - Auto/Visual Lisp: [Select]
  1. (defun ToString    (x / typex)
  2.   ;;  convert item to a string, if x is a real use
  3.   ;;  the highest possible precision, if x is a
  4.   ;;  string double quote it, if x is a list process
  5.   ;;  each item in the list appropriatel, otherwise
  6.   ;;  just hammer item with vl-princ-to-string
  7.   ;;   (ToString 1.0 )
  8.   (cond   ;;  it's a string, return it double quoted
  9.    ((eq 'str (setq typex (type x))) (strcat "\"" x "\""))
  10.    ;; it's a real, with decimal
  11.    ((and (eq 'real typex) (= (- x (fix x)) 0.0)); reali con parte decimale nulla
  12.     (setq oldsys# (getvar 'dimzin))
  13.     (setvar 'dimzin 1); variabile sistema dimzin a 1 consente di non sopprimere gli zeri finali nelle quote decimali
  14.     (setq res$ (rtos x 2 1)) ;_  numeri reali coincidenti con interi trasformati in stringhe con un solo decimale
  15.     (setvar 'dimzin oldsys#)
  16.     res$)
  17.    ;;  it's a real, covert to the highest possible
  18.    ;;  resolution string equivalent
  19.    ((eq 'real typex)
  20.     (rtos x
  21.           2
  22.           (if (zerop (- x (fix x)))
  23.        1
  24.        15)))
  25.    ;;  it's a list
  26.    ((eq 'list typex)
  27.     (if (vl-list-length x)
  28.       ;;  it's a normal list
  29.       (strcat (chr 40)
  30.          (ToString (car x))
  31.          (apply 'strcat (mapcar '(lambda (x) (strcat " " (ToString x))) (cdr x)))
  32.          (chr 41))
  33.       ;;  it's a dotted pair
  34.       (strcat (chr 40) (ToString (car x)) " . " (ToString (cdr x)) (chr 41))))
  35.    ;;  hammer down on everything else
  36.    ((vl-princ-to-string x))))
« Last Edit: November 19, 2018, 01:19:38 PM by kdub »