Jump to content

Rounding to 1 decimal point and removing 0's


ScoRm

Recommended Posts

(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??

Link to comment
Share on other sites

.

 


(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 by Emmanuel Delay
Link to comment
Share on other sites

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 by ronjonp
  • Like 1
Link to comment
Share on other sites

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"

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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