TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: hendie on March 22, 2007, 07:50:36 AM

Title: Forcing Decimal Places
Post by: hendie on March 22, 2007, 07:50:36 AM
It's been a while since I had to program in Lisp (and it's really really showing  :cry:)

this is doin' ma heid in mon.....
I am adding a list of numbers and I cannot get the result to show with 2 decimal places.

my UNITS is set to Decimal
PRECISION is set to 4
UNITMODE is 0 (tried it with 1 as well)

Code: [Select]
(setq X 17.00)
17.0
_$ (setq Z (rtos X 2 2))
"17"

what is the blatantly obvious bit that I'm missing ? please enlighten me oh great Lispers
Title: Re: Forcing Decimal Places
Post by: CADaver on March 22, 2007, 07:56:54 AM
Check UNITMODE
or
Try setting DIMZIN to 0


Found the following in Developer Help:
Quote
AutoLISP function rtos returns incorrect value (no zeros after decimal separator)
--------------------------------------------------------------------------------
Issue
You use the AutoLISP® function rtos to return a string representing the settings of the mode and precision for the units in the drawing. This string must be a whole number expressed with zeros after the decimal separator. However, the function seems to return an incorrect value. For example:
(setq z 100)
(rtos z 2 2))

You expect (rtos z 2 2)) to return 100.00, but sometimes the value returned is displayed as 100.

Solution
The AutoLISP function rtos takes information from four system variables: UNITMODE, DIMZIN, LUNITS and LUPREC. Usually, an incorrect value returned by rtos is caused by the DIMZIN variable being set to a value other than 4,3,2,1, or 0.

To resolve the problem, ensure that the DIMZIN variable is not set to 8 or 12.

The following information is from Help in the AutoCAD® software:
DIMZIN

Controls the suppression of zeros in the primary unit value. DIMZIN stores this value when you enter it on the command line or set it under Primary Units in the Annotation dialog box. DIMZIN values 0-3 affect feet-and-inch dimensions only.

0 Suppresses zero feet and precisely zero inches
1 Includes zero feet and precisely zero inches
2 Includes zero feet and suppresses zero inches
3 Includes zero inches and suppresses zero feet
4 Suppresses leading zeros in decimal dimensions (for example, 0.5000 becomes .5000)
8 Suppresses trailing zeros in decimal dimensions (for example, 12.5000 becomes 12.5)
12 Suppresses both leading and trailing zeros (for ex-ample, 0.5000 becomes 0.5)
Title: Re: Forcing Decimal Places
Post by: hendie on March 22, 2007, 08:22:08 AM
Thanks Alan, it was DIMZIN that did it

much appreciated
Title: Re: Forcing Decimal Places
Post by: Keith™ on March 22, 2007, 08:27:49 AM
interesting ... I never knew that .. I must remember to start checking dimzin in those pesky little lisp routines .. ;)
Title: Re: Forcing Decimal Places
Post by: whdjr on March 22, 2007, 08:55:36 AM
hmm... :-o...I didn't know Randy changed his name to Alan...
Title: Re: Forcing Decimal Places
Post by: Keith™ on March 22, 2007, 09:03:55 AM
DOH! .... Hendie must get his eyes checked ;)
Title: Re: Forcing Decimal Places
Post by: Maverick® on March 22, 2007, 09:28:15 AM
  Or he thinks Alan is a Cadaver.  :-o
Title: Re: Forcing Decimal Places
Post by: hendie on March 22, 2007, 09:37:14 AM
Ooops, my sincere apologies Randy, I thought it was CAB that responded.... ooh the shame

Thanks RANDY !!!
Title: Re: Forcing Decimal Places
Post by: CADaver on March 22, 2007, 12:26:47 PM
Ooops, my sincere apologies Randy, I thought it was CAB that responded.... ooh the shame

Thanks RANDY !!!
You're quite welcome.

BTW, Alan is one of the nicer things I've ever been called...

... especially around here.
Title: Re: Forcing Decimal Places
Post by: hendie on March 22, 2007, 03:15:31 PM
Randy, in my opinion, you are intelligent enough, respected enough, and creditable enough to be called an "Alan" ~  we're a very exclusive club ya know
Title: Re: Forcing Decimal Places
Post by: CADaver on March 23, 2007, 12:36:36 AM
Randy, in my opinion, you are intelligent enough, respected enough, and creditable enough to be called an "Alan" ~  we're a very exclusive club ya know
Thank you sir... I think. :)

For two years the music minister at church called me Jerry for some unknown reason, even though he had known me for years.  At first we'd all poke fun at him, but after a while I just answered to it.  Half the church still thinks my name is Jerry.
Title: Re: Forcing Decimal Places
Post by: CAB on March 26, 2007, 01:55:57 PM
Hendie / Alan is one of the Good guys too.

I would have answered but was out of town.
So better late than never.
Code: [Select]
;;  Pad.lsp    CAB 07/13/06
;;  Pad right with 0 to precision plc
;;  No Rounding!
(defun pad (str plc / sp)
  (setq tmp (reverse (vl-string->list str)))
  (if (not (vl-position 46 tmp))
    (setq tmp (cons 46 tmp))
  )
  (cond
    ((= (vl-position 46 tmp) plc))
    ((< (vl-position 46 tmp) plc)
     (repeat (- plc (vl-position 46 tmp))
       (setq tmp (cons 48 tmp))
     )
    )
    (t
     (repeat (- (vl-position 46 tmp) plc)
       (setq tmp (cdr tmp))
     )
    )
  )
  (vl-list->string (reverse tmp))
)