ScoRm Posted October 5, 2018 Posted October 5, 2018 (defun c:rrounding (/ obj) (and (setq obj (car (entsel "\nPick text object :"))) (setq obj (vlax-ename->vla-object obj)) (wcmatch (vla-get-objectname obj) "AcDb*Text") (vla-put-textstring obj (rtos (lm:roundto (atof (vla-get-textstring obj)) 2) 2 2)) ) (princ) ) ;; Multiple selection (defun c:foo (/ o s) (if (setq s (ssget ":L" '((0 . "*text")))) (foreach a (vl-remove-if 'listp (mapcar 'cadr (ssnamex s))) (setq o (vlax-ename->vla-object a)) (vla-put-textstring o (rtos (lm:roundto (atof (vla-get-textstring o)) 2) 2 2)) ) ) (princ) ) (defun lm:roundto (n p) (lm:roundm n (expt 10.0 (- p)))) (defun lm:roundm (n m) (* m (atoi (rtos (/ n (float m)) 2 0)))) Can someone help me what to change on this code?? i need to round off into 1 decimal place only, and also remove all 0's at the begining ex: 0.10... i need it to be .10 only 0.14... i need it to be .14 only the one decimal place i really need is for greater than 1 numbers like example 1.59... i need it to be 1.6 1.06... i need it to be 1.1 1.04... i need it to be 1.0 please help?? Quote
Emmanuel Delay Posted October 5, 2018 Posted October 5, 2018 (edited) . (defun c:test ( / ) (princ (roundspecial 0.14)) (princ "\n") (princ (roundspecial 1.59)) (princ "\n") (princ (roundspecial 1.06)) (princ "\n") (princ (roundspecial 1.04)) (princ "\n") (princ) ) (defun roundspecial (num / ) (if (and (> num 0) (< num 1)) (substr (rtos num 2 1) 2 ) ;; remove the first character, which is a 0 (rtos num 2 1) ) ) Edited October 5, 2018 by Emmanuel Delay Quote
ronjonp Posted October 5, 2018 Posted October 5, 2018 (edited) Hint (VL-STRING-LEFT-TRIM "0" "0.10") You also need to change the precision for Lee's round function to 1: (lm:roundto (atof (vla-get-textstring o)) 1) Edited October 5, 2018 by ronjonp 1 Quote
Lee Mac Posted October 5, 2018 Posted October 5, 2018 Here's another way to approach it - (defun myrtos ( x / dim rtn ) (setq dim (getvar 'dimzin)) (setvar 'dimzin 4) (setq rtn (vl-catch-all-apply 'rtos (list x 2 (if (< x 1) 2 1)))) (setvar 'dimzin dim) (if (not (vl-catch-all-error-p rtn)) rtn) ) For example: _$ (myrtos 1.59) "1.6" _$ (myrtos 1.06) "1.1" _$ (myrtos 1.04) "1.0" _$ (myrtos 0.14) ".14" Quote
tombu Posted October 5, 2018 Posted October 5, 2018 5 hours ago, ronjonp said: Hint (VL-STRING-LEFT-TRIM "0" "0.10") You also need to change the precision for Lee's round function to 1: (lm:roundto (atof (vla-get-textstring o)) 1) https://www.theswamp.org/index.php?topic=50109.msg552917#msg552917 I keep links to tricks I've picked up it the code I've used them in to give credit where credit is due. Quote
ScoRm Posted October 6, 2018 Author Posted October 6, 2018 16 hours ago, ronjonp said: Hint (VL-STRING-LEFT-TRIM "0" "0.10") You also need to change the precision for Lee's round function to 1: (lm:roundto (atof (vla-get-textstring o)) 1) i will try this first, thank you for the help! 11 hours ago, Lee Mac said: Here's another way to approach it - (defun myrtos ( x / dim rtn ) (setq dim (getvar 'dimzin)) (setvar 'dimzin 4) (setq rtn (vl-catch-all-apply 'rtos (list x 2 (if (< x 1) 2 1)))) (setvar 'dimzin dim) (if (not (vl-catch-all-error-p rtn)) rtn) ) For example: _$ (myrtos 1.59) "1.6" _$ (myrtos 1.06) "1.1" _$ (myrtos 1.04) "1.0" _$ (myrtos 0.14) ".14" thank you sir, but i havent figure out how to add this on the code lol! but i will try and figure it out Quote
Lee Mac Posted October 6, 2018 Posted October 6, 2018 3 hours ago, ScoRm said: thank you sir, but i havent figure out how to add this on the code lol! but i will try and figure it out Copy the function definition to the end of your program, and then replace: (rtos (lm:roundto ... )) With: (myrtos ... ) (Removing a closing parenthesis from the right-hand side) 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.