shakuhachi Posted August 19, 2011 Posted August 19, 2011 Hi gurus. How can I drop the zero ".0" is the sum is "5.0". I need all the trailing zeros to be removed. eq. sum = 5.0 -> 5 sum = 5.40 -> 5.4 sum = 6.4800 -> 6.48 sum = 7.514500 - 7.5145 Thanks (defun c:test () (setq a (getreal)) (setq b (getreal)) (princ (strcat (rtos (+ a b) 2 1) "abc")) (princ) ) Quote
shakuhachi Posted August 19, 2011 Author Posted August 19, 2011 values are not distance but volume. I'm getting value something like 10'-8". (setvar 'dimzin Quote
BlackBox Posted August 19, 2011 Posted August 19, 2011 values are not distance but volume. I'm getting value something like 10'-8". Looks an awful lot lot a measurement of length (distance) to me (ten feet-eight inches). Anyway, you can also try vl-princ-to-string... example: _$ (vl-load-com) _$ (vl-princ-to-string 7.514500) "7.5145" _$ Edit: This would allow for you to retain your DIMZIN setting, without the need for error checking & restore. HTH Quote
BlackBox Posted August 19, 2011 Posted August 19, 2011 Great info Renderman (vl-string-right-trim ".0" <string>) ^^ Another great option. I thought of that one first, but for some reason (mis-)remembered that it would remove all instances of the character-set in one swoop... thought I remembered needing to use a while+trim combo once before. Guess that's what I get for not trying it for myself! LoL Quote
shakuhachi Posted August 19, 2011 Author Posted August 19, 2011 Thanks guys it works but combination of both. (vl-string-right-trim ".0" (vl-princ-to-string x)) Quote
shakuhachi Posted August 19, 2011 Author Posted August 19, 2011 Why not use rtos?I did, look on post # 1. The output I get is in distance something like 10'-8". I need to remove all trailing zeros and period if any on the total. If the output value is a whole number I want it to be an integer and if the value is a real number, I want to remove all trailing zero. Why not use rtos? Quote
BlackBox Posted August 19, 2011 Posted August 19, 2011 Why not use rtos? 1+ (vl-string-right-trim "0" (rtos x)) Quote
shakuhachi Posted August 19, 2011 Author Posted August 19, 2011 (setq x 15.0) (vl-string-right-trim "0" (rtos x)) "1'-3\"" value I get, needs to be 15 1+ (vl-string-right-trim "0" (rtos x)) Quote
BlackBox Posted August 19, 2011 Posted August 19, 2011 (setq x 15.0) (vl-string-right-trim "0" (rtos x)) "1'-3\"" value I get, needs to be 15 Then perhaps you need to conditionally parse your value -->> into the desired format. Perhaps this will help: (defun FEET (s) ;; © RenderMan, 2011 (substr s 1 (vl-string-search "'" s))) Extract inches from string: (defun INCH (s / i) ;; © RenderMan, 2011 (substr s (setq i (+ 2 (vl-string-search "-" s))) (- (strlen s) i))) Examples: _$ (feet "10'-11\"") "10" _$ (feet "10000'-11\"") "10000" _$ (inch "10'-11\"") "11" _$ (inch "10'-1111\"") "1111" _$ HTH Quote
Lee Mac Posted August 19, 2011 Posted August 19, 2011 (setq x 15.0) (vl-string-right-trim "0" (rtos x)) "1'-3\"" value I get, needs to be 15 (vl-string-right-trim ".0" (rtos x 2)) Quote
shakuhachi Posted August 20, 2011 Author Posted August 20, 2011 didn't work either. try: Command: (setq x 1510.0) 1510.0 Command: (vl-string-right-trim ".0" (rtos x 2)) "151" (vl-string-right-trim ".0" (rtos x 2)) Quote
BlackBox Posted August 20, 2011 Posted August 20, 2011 Which format is it "1510.0" or "1'-3\""??? Quote
pBe Posted August 20, 2011 Posted August 20, 2011 (defun supp0 (str / a ) (setq a (reverse (vl-string->list str))) (while (= (car a) 48) (setq a (cdr a)) ) (setq a (if (= (car a) 46)(cdr a) a)) (vl-list->string (reverse a)) ) (supp0 "5.40") "5.4" (supp0 (rtos 6.4800 2)) "6.48" (supp0 "7.514500") "7.5145" (setq x 1510.0) (supp0 (rtos x 2)) "1510" Quote
Lee Mac Posted August 20, 2011 Posted August 20, 2011 didn't work either.try: Command: (setq x 1510.0) 1510.0 Command: (vl-string-right-trim ".0" (rtos x 2)) "151" Oh - good catch, I overlooked that behaviour with vl-string-*-trim. A quick fix would be: (vl-string-right-trim "." (vl-string-right-trim "0" (rtos x 2))) But this still assumes DIMZIN is set to something other than say, 8 (where trailing zeros are removed anyway). So perhaps this is better: (defun _TrimTrailingZeros ( s ) (if (vl-string-position 46 s nil t) (vl-string-trim "." (vl-string-right-trim "0" s)) s ) ) DIMZIN=0 _$ (_TrimTrailingZeros (rtos 1510.0 2)) "1510" DIMZIN=8 _$ (_TrimTrailingZeros (rtos 1510.0 2)) "1510" Quote
Lee Mac Posted August 20, 2011 Posted August 20, 2011 (defun supp0 (str / a ) (setq a (reverse (vl-string->list str))) (while (= (car a) 48) (setq a (cdr a)) ) (setq a (if (= (car a) 46)(cdr a) a)) (vl-list->string (reverse a)) ) Nice idea pBe, but with DIMZIN=8: _$ (supp0 (rtos 1510.0 2)) "151" Quote
shakuhachi Posted August 20, 2011 Author Posted August 20, 2011 Thank you guys, that works. I knew it was simple as abc... Now on my next routine... will post again if I get stuck. Thanks again! 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.