Jump to content

Recommended Posts

Posted

Using a lisp, i insert mtexts, with (rtos mtext 2 2); how to supress 0 when my mtext is... 2.00? - to be 2 ?

Posted

One correction - seems that you are talking about a variable named mtext, not the AutoCAD entity; thus the thread title is a little confusing.

 

To solve your issue, one solution is to take advantage of DIMZIN system variable.

(setq oldDimZin (getvar "DIMZIN"))
(setvar "DIMZIN" 
(rtos mtext 2 2)
(setvar "DIMZIN" oldDimZin)

Please don't forget to restore it later - it may affect your dimensions too.

Posted

Tharwat, then how about such case?

(rtos 2.265 2 0)

OP was looking to supress zeros...

Posted

I guess the OP wants to suppress the decimals to zero , isn't that correct ?

Posted
With dimzin is ok.

 

Why you would go with system variable since the function rtos could solve the issue , beside that , you would be in need of error function to reset the Sys Vars back if a user cancel the the codes before finishing the task.

Posted
With dimzin is ok. Thanks!

You're welcome! Again, use it with care.

Posted
Why you would go with system variable since the function rtos could solve the issue

I will say that RTOS alone cannot solve OP's issue - he/she need to parse the resulted string to get rid of tailing zeros:

2.127  -->  "2.13"
2.100  -->  "2.1"
2.000  -->  "2"

 

you would be in need of error function to reset the Sys Vars back if a user cancel the the codes before finishing the task.

That's a very good observation.

Posted

You could always just trim off all zeros at the end (as well as the decomal point if at the end):

(vl-string-right-trim "0." (rtos mtext 2 2))

Then you don't need to set DimZin at all. Never mind reseting it in an error trap.

Posted

Watch out for this

 

(vl-string-right-trim "0." (rtos 20.00 2 2))

"2"

Posted

If DIMZIN is already set to 8 in the user's environment, you will have trouble using vl-string-right-trim, e.g.:

 

DIMZIN = 8
(vl-string-right-trim "0" (rtos 20.00 2 2))
2

I propose:

 

(defun _rtos ( real prec / dimzin result )
   (setq dimzin (getvar 'dimzin))
   (setvar 'dimzin 
   (setq result (vl-catch-all-apply 'rtos (list real prec)))
   (setvar 'dimzin dimzin)
   (if (not (vl-catch-all-error-p result))
       result
   )
)

_$ (_rtos 20.00 2)
"20"

 

Also refer:

 

http://www.cadtutor.net/forum/showthread.php?68614&p=469459&viewfull=1#post469459

Posted

This one is up to none zero or decimal comma or point .

 

(defun _SuppressDecimals (string / l)
 ;;; Tharwat 15.05.2012 ;;;
 (if (eq (type (read string)) 'REAL)
   (while (eq (last (setq l (vl-string->list string))) 48)
     (setq string (substr string 1 (1- (length l))))
   )
 )
 string
)

example ..

(_SUPPRESSDECIMALS "20.00")

Posted

@Tharwat,

 

Why convert the string to a list with each iteration of the while loop?

 

Consider:

 

(defun _SuppressDecimals2 ( string / l )
   (setq l (reverse (vl-string->list string)))
   (while (= 48 (car l)) (setq l (cdr l)))
   (vl-list->string (reverse l))
)

Posted
@Tharwat,

 

Why convert the string to a list with each iteration of the while loop?

 

 

That's correct .

 

You always have something better :thumbsup:

Posted

(_SUPPRESSDECIMALS "22.00")
"22."
(_SUPPRESSDECIMALS2 "20.00")
"20."

 

Whats with the dot "." ?

Posted
(_SUPPRESSDECIMALS "22.00")
"22."
(_SUPPRESSDECIMALS2 "20.00")
"20."

 

Whats with the dot "." ?

 

With the adds of the following to my previous code , would do the trick .:)

 

(setq string (vl-string-right-trim "." string))

Posted

Ah! Yes, forgot about that! :oops:

 

Lee, if I could make a small mod to your code (perhaps o:))

(defun _rtos ( real prec / dimzin result )
 (setq dimzin (getvar 'dimzin))
 (setvar 'dimzin 
 (setq result (vl-catch-all-apply 'rtos (list real prec)))
 (setvar 'dimzin dimzin)
 (if (vl-catch-all-error-p result)
   (vl-exit-with-error (vl-catch-all-error-message result))
   result))

Posted
Lee, if I could make a small mod to your code (perhaps o:))

 

I usually prefer a nil return when the function is unsuccessful, but your addition would better emulate the standard behaviour of the rtos function.

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...