how to determine if a string is a valid floating number, ie does not contain any invalid chars etc
i came out with this, but seems too complicated, i am suspecting an easier solution
(setq str "11.25")
(wcmatch str "*@*,[`~```@`#`$`%`^`&`*`(`)`-`_`+`=`[`]`{`}`|`\`;`:`\"`'`,`<`>`?`/")
TIA
Solved! Go to Solution.
Solved by SomeBuddy. Go to Solution.
Hi,
You could use something like this:
(defun chkval (str)
(if (numberp (read str))
(alert "It's a number")
(alert "It's not a number")
)
(princ)
)
Usage:
(chkval "11.25")
(chkval "11.qr")
HTH
it helps, although i am intigued how a string like "12,44" a comma instead of a period is handled correctly by lisp,
(read "12,44") returns 12 but then numberp function is able to reject it.
it works, thanks
m
You're welcome.
(read "12,44") returns 12,44 on my machine and this is not a valid number for the LISP interpretor, since it "knows" that the decimal separator is the period and not the comma.
@SomeBuddy wrote:....
(defun chkval (str)
(if (numberp (read str))
(alert "It's a number")
(alert "It's not a number")
)
(princ)
)....
Might the strings ever include spaces? For instance:
(chkval "30 days")
says it's a number.
Hmm,
This (read...) function is so dumb. The following will do better, except for the case where the string is entered using the scientific units type, i.e. using the "e-" or "e+" notation, which is quite rare:
(defun chknum (str) (if (vl-remove-if '(lambda (x)(or (wcmatch (chr x) "#")(= (chr x) "."))) (vl-string->list str) ) (alert "It's not a number.") (alert "It's a number.") ) (princ) )
had to add to the if condition
(if (and ...
(< (length (vl-remove-if-not '(lambda (x)(= x 46))(vl-string-list str))) 2) ...
to allow only 1 period or no period
still think there should be an easier way, a combo of rtos / atof / distof /atoi / itoa ?
I understand the two periods issue, but I don't understand the no period one. Maybe this one is good enough:
(defun chknum (str) (if (not (distof str)) (alert "It's not a number.") (alert "It's a number.") ) (princ) )
@SomeBuddy wrote:I understand the two periods issue, but I don't understand the no period one. Maybe this one is good enough:
.... (if (not (distof str)) ....
No periods = integers, which they do want to allow. But testing for quantity of periods doesn't appear to be necessary, given that (distof) seems to be the magic function that does the trick [at least, I haven't hit on any type of string content that fools it.]
some minor issues w/ distof ... a leading space, inch/foot signs, forward slashes (of course depending on the units setting)
to make clear my intentions, i am trying to test input in a dialog edit box for a valid number, be it integer or float.
thank you Kent & Some
m
<No periods = integers, which they do want to allow.>
I know what no period means, but what I was saying is that I don't see why is this alternative mentionned as an issue, while for me, the solution posted at that time was doing fine with integers.
You're welcome. I was supposing that this is what you want to do with it, i.e. validating some user input 🙂
@SomeBuddy wrote:....
I know what no period means, but what I was saying is that I don't see why is this alternative mentionned as an issue, while for me, the solution posted at that time was doing fine with integers.
Integers weren't an "issue," but one of the approved conditions ["allow only 1 period or no period"]. It was only a string with more than one period that was the issue, because that routine considered such a string to represent a number. Their added check was only to ensure that such a string would not pass.
The inch/foot signs and the forward slashes should be controlled using the proper mode (units settings) but for the leading space(s) you can improuve the code a little bit using something like:
(if (distof (vl-string-trim " " str)) (alert "It's a number.") (alert "It's not a number.") )
Regards