Jump to content

Recommended Posts

Posted

When I type (rtos 2.5 2 2) at the command line I get "2.5" - I was expecting "2.50"

 

I need the value displayed to two decimal places - "2.50"

 

LUNITS is set to 2

 

Am I using the wrong funtion ? All help is appreciated.

Posted

The easiest solution is to change the value of DIMZIN before performing the rtos operation:

 

([color=blue]defun[/color] _rtos ( real units prec [color=blue]/ [/color]dimzin result )
 
 ([color=blue]setq [/color]dimzin ([color=blue]getvar [/color]'DIMZIN))
 ([color=blue]setvar [/color]'DIMZIN 0)

 ([color=blue]setq [/color]result ([color=blue]rtos [/color]real units prec))

 ([color=blue]setvar [/color]'DIMZIN dimzin)  
 result
)

However, just for fun, I wrote this a while back:

 

[color=GREEN];;------------------------=={ rtos }==------------------------;;[/color]
[color=GREEN];;                                                            ;;[/color]
[color=GREEN];;  Subfunction designed to produce a consistent rtos return, ;;[/color]
[color=GREEN];;  independent of the DIMZIN System Variable.                ;;[/color]
[color=GREEN];;                                                            ;;[/color]
[color=GREEN];;  [ Currently restricted to Decimal Format only ]           ;;[/color]
[color=GREEN];;------------------------------------------------------------;;[/color]
[color=GREEN];;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;[/color]
[color=GREEN];;------------------------------------------------------------;;[/color]
[color=GREEN];;  Arguments:                                                ;;[/color]
[color=GREEN];;  real - real number to convert to a string                 ;;[/color]
[color=GREEN];;  prec - precision to which the real should be displayed    ;;[/color]
[color=GREEN];;------------------------------------------------------------;;[/color]
[color=GREEN];;  Returns:  String representing the supplied real           ;;[/color]
[color=GREEN];;------------------------------------------------------------;;[/color]

([color=BLUE]defun[/color] LM:rtos ( real prec ) ([color=BLUE]vl-load-com[/color])
 [color=GREEN];; © Lee Mac 2011[/color]
 (
   ([color=BLUE]lambda[/color] ( l )
     ([color=BLUE]repeat[/color] ([color=BLUE]-[/color] prec ([color=BLUE]cond[/color] ( ([color=BLUE]vl-position[/color] 46 l) ) ( ([color=BLUE]setq[/color] l ([color=BLUE]cons[/color] 46 l)) 0 )))
       ([color=BLUE]setq[/color] l ([color=BLUE]cons[/color] 48 l))
     )
     ([color=BLUE]vl-string-right-trim[/color] [color=MAROON]"."[/color]
       ([color=BLUE]vl-list->string[/color]
         ([color=BLUE]cond[/color]
           ( ([color=BLUE]=[/color] 46 ([color=BLUE]car[/color] ([color=BLUE]setq[/color] l ([color=BLUE]reverse[/color] l))))
             ([color=BLUE]cons[/color] 48 l)
           )
           ( ([color=BLUE]and[/color] ([color=BLUE]=[/color] 45 ([color=BLUE]car[/color] l)) ([color=BLUE]=[/color] 46 ([color=BLUE]cadr[/color] l)))
             ([color=BLUE]cons[/color] 45 ([color=BLUE]cons[/color] 48 ([color=BLUE]cdr[/color] l)))
           )
           ( l )
         )
       )
     )
   )
   ([color=BLUE]reverse[/color] ([color=BLUE]vl-string->list[/color] ([color=BLUE]rtos[/color] real 2 prec)))
 )
)

Posted

Thanks Lee for you quick post.

(defun _rtos ( real units prec / dimzin result )

 

(setq dimzin (getvar 'DIMZIN))

(setvar 'DIMZIN 0)

 

(setq result (rtos real units prec))

 

(setvar 'DIMZIN dimzin)

result

)

is what I needed.

 

Thanks again.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...