Jump to content

Recommended Posts

Posted (edited)

How do I remove leading Number after the decimal point from LINE elevation?
 

(defun c:test (/ ss obj elev lay)
(setq ss (ssget (list (cons 0 "LINE"))))
 (repeat (setq x (sslength ss))
   (setq obj (ssname ss (setq x (- x 1))))
   (setq elev (caddr (cdr (assoc 10 (entget obj)))))
   (setq lay (rtos elev 2 1))
   (command "-layer" "m" lay "")
   (command "._change" obj "" "p" "la" lay "")
 )
(princ)
)

Im so stupid, I know >.<"

Edited by ktbjx
Posted (edited)

So you want 0.1 to be .1 ? You convert to a string above code Lay = "0.1" , check 1st character if 0 then remove using "substr" and "strlen" function. Very easy.

Edited by BIGAL
Posted
5 minutes ago, BIGAL said:

So you want 0.1 to be .1 ? You convert to a string above code Lay = "0.1" , check 1st character if 0 then remove using "substr" and "strlen" function. Very easy.

im sorry, I made a mistake about my question,
I want to remove all numbers after the decimal point.

so for your example

0.1 would be 0

1.4000 will be 1

698.345 will be 698

 

just remove all numbers after decimal

Posted

Rtos 2 0 will remove but be careful it will possibly round the answer else use rtos 2 1 then just remove the last two characters from the string lay using substr.

Posted (edited)
6 hours ago, ktbjx said:

I want to remove all numbers after the decimal point.

so for your example

0.1 would be 0

1.4000 will be 1

698.345 will be 698

just remove all numbers after decimal

 

1.55 → 1 or 1.55 → 2 ?

Edited by Lee Mac
Posted
55 minutes ago, Lee Mac said:

1.55 → 1 or 1.55 → 2 ?

 

I think he wants this algorithm of "rounding".

Posted (edited)

In that case, change:

(setq lay (rtos elev 2 1))

To:

(setq lay (itoa (fix (+ 1e-8 elev))))

 

Edited by Lee Mac
Posted

Neat trick, Lee! :thumbsup:

(mapcar 
  '(lambda (x) (fix (+ 1e-8 x))) 
  '(2.1 2.9 3.14 0.15)
)
>> (2 2 3 0)

 

Posted (edited)
2 hours ago, Grrr said:

Neat trick, Lee! :thumbsup:


(mapcar 
  '(lambda (x) (fix (+ 1e-8 x))) 
  '(2.1 2.9 3.14 0.15)
)
>> (2 2 3 0)

 

 

Thanks - of course, fix on its own would yield the same -

_$ (mapcar 'fix '(2.1 2.9 3.14 0.15))
(2 2 3 0)

But the inclusion of the 1e-8 'fuzz' accounts for rounding of doubles and avoids situations like this:

_$ (fix (- 8.2 0.2))
7

Though, when used in practice, one should really account for the possibility of negatives since:

_$ (fix (+ -2 1e-8))
-1

And so, perhaps a better suggestion would be:

(setq lay (itoa (fix ((if (minusp elev) - +) elev 1e-8))))

 

Edited by Lee Mac
  • Like 1

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