Author Topic: rtos and rounding  (Read 3163 times)

0 Members and 1 Guest are viewing this topic.

DanB

  • Bull Frog
  • Posts: 367
rtos and rounding
« on: August 06, 2018, 10:35:42 AM »
Can someone shed some light on the apparent rounding or display of the following code. Ideally I would like the reported information not to round and display the number of decimal places as shown.

Code: [Select]
(defun c:test1 (/ calcarea)           
 (setq calcarea 43559.999999)
 (prompt (strcat "\n Variable is:    "  (rtos calcarea); this displays "43559.999999"
                  "\n "
                  "\n Area:    "  (rtos calcarea 2 2); this displays "43560", why not 43559.99
                  "\n "
                  "\n Area:    "  (rtos calcarea 2 4); this displays "43560", why not 43559.9999
                  "\n "
                  "\n Area:    "  (rtos calcarea 2 6))) ; this displays "43559.999999"
 (princ)
)

tombu

  • Bull Frog
  • Posts: 289
  • ByLayer=>Not0
Re: rtos and rounding
« Reply #1 on: August 06, 2018, 10:54:25 AM »
My results were as expected:
Command: (rtos calcarea 2 2)
"43560.00"
Command: (rtos calcarea 2 4)
"43560.0000"
Command: (rtos calcarea 2 6)
"43559.999999"
Command: (rtos calcarea 2 5)
"43560.00000"
Tom Beauford P.S.M.
Leon County FL Public Works - Windows 7 64 bit AutoCAD Civil 3D

Crank

  • Water Moccasin
  • Posts: 1503
Re: rtos and rounding
« Reply #2 on: August 06, 2018, 11:10:05 AM »
The DIMZIN variable affects real-to-string conversions performed by the AutoLISP rtos  and angtos  functions.
Vault Professional 2023     +     AEC Collection

DanB

  • Bull Frog
  • Posts: 367
Re: rtos and rounding
« Reply #3 on: August 06, 2018, 01:08:15 PM »
DIMZIN is a variable for suppressing zeros from a given value.

To clarify my problem/request: if the input value is "43559.999999" - I would like to be able to return the value "43559.99" by using (rtos calcarea 2 2). Where am I going wrong?

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: rtos and rounding
« Reply #4 on: August 06, 2018, 01:18:31 PM »
To clarify my problem/request: if the input value is "43559.999999" - I would like to be able to return the value "43559.99" by using (rtos calcarea 2 2). Where am I going wrong?

The value 43559.999999 rounded to 2 decimal places is 43560

It sounds like you actually want to truncate to 2 decimal places?

Code - Auto/Visual Lisp: [Select]
  1. _$ (setq x 43559.999999)
  2. 43560.0 ;; Console automatically rounds double output
  3. _$ (rtos (- x (rem x 0.01)) 2 2)
  4. "43559.99"

DanB

  • Bull Frog
  • Posts: 367
Re: rtos and rounding
« Reply #5 on: August 06, 2018, 01:54:24 PM »
Yes, truncating the value should achieve my goals. Thanks again.

Not sure I fully understand what Lee provided (the dash or negative proceeding the "x" in line 3??) but going to see what I can learn from it, thank you.

Code: [Select]
(rtos (- x (rem x 0.01)) 2 2)

Edit - Nevermind, with some minor testing I'm starting to see what's happening in line #3 - thanks.
« Last Edit: August 06, 2018, 02:04:00 PM by DanB »

d2010

  • Bull Frog
  • Posts: 326
Re: rtos and rounding
« Reply #6 on: August 13, 2018, 01:52:39 PM »
I fully understand what Lee provided (the dash or negative proceeding the "x" in line 3??) but going to see what I can learn from it, thank you.

;;bellow. We use this code . You can translate manually to autolisp..
;;This solution is for zero-trailing from autocadR14.
Code: [Select]
(defun dfn_real_rtos (x151 / rr aa bb un od dz xx)
/*c2s:
       rr=nil,
       xx=x151,
       un=getvar(_T("LUNITS")),
       dz=_T("DIMZIN");
       if ((un==4)||(un==5)) rr=rtos(xx);
            else { od=getvar(dz);
                   setvar(dz,(od&(~8)));
                   aa=rtos(xx);
                   setvar(dz,(od|8));
                   bb=rtos(xx,un,15);
                   setvar(dz,od);
                   rr=equal(distof(aa),distof(bb),0.000001)?aa:bb;
                 };
*/
rr)
:knuppel2: