SELFCAD Posted May 15, 2012 Posted May 15, 2012 Using a lisp, i insert mtexts, with (rtos mtext 2 2); how to supress 0 when my mtext is... 2.00? - to be 2 ? Quote
MSasu Posted May 15, 2012 Posted May 15, 2012 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. Quote
MSasu Posted May 15, 2012 Posted May 15, 2012 Tharwat, then how about such case? (rtos 2.265 2 0) OP was looking to supress zeros... Quote
Tharwat Posted May 15, 2012 Posted May 15, 2012 I guess the OP wants to suppress the decimals to zero , isn't that correct ? Quote
Tharwat Posted May 15, 2012 Posted May 15, 2012 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. Quote
MSasu Posted May 15, 2012 Posted May 15, 2012 With dimzin is ok. Thanks! You're welcome! Again, use it with care. Quote
MSasu Posted May 15, 2012 Posted May 15, 2012 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. Quote
irneb Posted May 15, 2012 Posted May 15, 2012 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. Quote
pBe Posted May 15, 2012 Posted May 15, 2012 Watch out for this (vl-string-right-trim "0." (rtos 20.00 2 2)) "2" Quote
Lee Mac Posted May 15, 2012 Posted May 15, 2012 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 Quote
Tharwat Posted May 15, 2012 Posted May 15, 2012 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") Quote
Lee Mac Posted May 15, 2012 Posted May 15, 2012 @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)) ) Quote
Tharwat Posted May 15, 2012 Posted May 15, 2012 @Tharwat, Why convert the string to a list with each iteration of the while loop? That's correct . You always have something better Quote
pBe Posted May 15, 2012 Posted May 15, 2012 (_SUPPRESSDECIMALS "22.00") "22." (_SUPPRESSDECIMALS2 "20.00") "20." Whats with the dot "." ? Quote
Tharwat Posted May 15, 2012 Posted May 15, 2012 (_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)) Quote
irneb Posted May 15, 2012 Posted May 15, 2012 Ah! Yes, forgot about that! Lee, if I could make a small mod to your code (perhaps ) (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)) Quote
Lee Mac Posted May 15, 2012 Posted May 15, 2012 Lee, if I could make a small mod to your code (perhaps ) I usually prefer a nil return when the function is unsuccessful, but your addition would better emulate the standard behaviour of the rtos function. Quote
Recommended Posts
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.