Jump to content

Recommended Posts

Posted

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

  • Replies 21
  • Created
  • Last Reply

Top Posters In This Topic

  • shakuhachi

    7

  • Lee Mac

    6

  • BlackBox

    6

  • Lt Dan's legs

    2

Top Posters In This Topic

Posted

values are not distance but volume. I'm getting value something like 10'-8".

 

 
(setvar 'dimzin 

Posted
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

Posted
Great info Renderman

 

:beer:

 

(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! :facepalm: LoL

Posted

Thanks guys it works but combination of both.

 

(vl-string-right-trim ".0" (vl-princ-to-string x))

Posted

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?
Posted
Why not use rtos?

 

1+

 

(vl-string-right-trim "0" (rtos x))

Posted

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

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

Posted
(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))

Posted

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

Posted

Which format is it "1510.0" or "1'-3\""???

Posted

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

Posted
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"

Posted
 (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"

Posted

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!

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