Jump to content

Test if integer?


chavlji

Recommended Posts

Do you mean:

 

Command: (atoi "bla bla bla")
0

?

 

(defun atoi2(str)
 (if(distof str)(atoi str))
 ); end of atoi2

 

Command: (atoi2 "bla bla bla")
nil

 

Command: (atoi2 "8")
8

 

Command: (atoi2 "87.345123453")
87

Link to comment
Share on other sites

Sometimes 0 would be a valid return...

 

On older way:

 

[b][color=BLACK]([/color][/b]defun testistr [b][color=FUCHSIA]([/color][/b]s / tmp[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]setq tmp [color=#2f4f4f]""[/color][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]repeat [b][color=NAVY]([/color][/b]strlen s[b][color=NAVY])[/color][/b]
         [b][color=NAVY]([/color][/b]setq tmp [b][color=MAROON]([/color][/b]strcat tmp [color=#2f4f4f]"#"[/color][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]wcmatch s tmp[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

-David

Link to comment
Share on other sites

Sometimes 0 would be a valid return...

 

On older way:

 

[b][color=BLACK]([/color][/b]defun testistr [b][color=FUCHSIA]([/color][/b]s / tmp[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]setq tmp [color=#2f4f4f]""[/color][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]repeat [b][color=NAVY]([/color][/b]strlen s[b][color=NAVY])[/color][/b]
         [b][color=NAVY]([/color][/b]setq tmp [b][color=MAROON]([/color][/b]strcat tmp [color=#2f4f4f]"#"[/color][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]wcmatch s tmp[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

-David

 

 

Great idea David - love it. :) So many ways to accomplish the same thing :)

Link to comment
Share on other sites

(defun numtxt  (str)
 (vl-every '(lambda (x) (<= 48 x 57)) (vl-string->list str)))

Returns T or nil

 

CAB, would you say that vl-some should be used then, instead of vl-every?

Link to comment
Share on other sites

Sorry, jumping in and out of this thread over several days, I forgot what was & wasn't posted.

This morning I revisited the entire thread & see the Lee had already posted this test. I also

see that the OP said the string was comprised of numbers only. My post like Lee's is just a test

to confirm that there is at lease one number embedded in the string. Not a solution for the OP.

 

I use this to convert a number from a string. (Must be a valid number)

 

  ;;+++++++++++++++++++++++++++++++
 ;;  convert the text to a number or nil
 ;;+++++++++++++++++++++++++++++++
 (defun txt2num (txt / num)
   (or (setq num (distof txt 5))
       (setq num (distof txt 2))
       (setq num (distof txt 1))
       (setq num (distof txt 4))
       (setq num (distof txt 3))
   )
   num
 )

 

Returns nil if not a valid number.

Link to comment
Share on other sites

CAB,

You can avoid the allocation and destruction of a variable inside your support procedure by using a COND.

 

(defun txt2num ( txt )
 (cond
   ((distof txt 5))
   ((distof txt 2))
   ((distof txt 1))
   ((distof txt 4))
   ((distof txt 3))
 ) 
)

Link to comment
Share on other sites

I dunno, i didn't read the thread i just read the last post. ...wait a min? DISTOF works with strings and converts them to reals. ITOA converts an int to a string. Did you mean ATOI?

Link to comment
Share on other sites

Easy way:

 

(defun ITest(Str)
 (=(distof Str 2)(atoi Str))
 )

 

Testing:

 

_$ (ITest "5")
T
_$ (ITest "-50")
T
_$ (ITest "5.000008")
nil
_$ (ITest "5blablabla")
nil
_$ (ITest "0")
T

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