|
|||
|
Hi
Would anyone know of a better way to check if a variable is a number. I've written the code below which works set a variable to equal an empty string if its not a number. ; Test program to check that a variable is either ; a whole or a decimal number. If not then set number to equal " ; S NUMBER="345" I NUMBER'?1N.N d .. I NUMBER'?1N.N1"."1N.N S NUMBER=""; check if decimal number .. q W NUMBER Q Much appreciated if anyone knows of a better (or tried and tested) way of checking if variable NUMBER is actually a number or not. Cheers David |
|
|
||||
|
||||
|
|
|
|||
|
Well, it depends on what you mean by number....
If you want to confirm it is a real, rational number with no leading 0's, no trailing 0's, no unnecesary + at the beginning then: IF +NUMBER'=NUMBER W "NOT A NUMBER" would work. If you want to confirm that NUMBER only contains characters valid for numbers, but want leading/trailing zeros and + to be valid, you could use: IF $TR(NUMBER,"1234567890.+-","")'="" W "NOT A NUMBER" Would work, but it would still allow for multiple ., -, or +, or misplaced - or + which might cause you grief, so you might need IF $TR(NUMBER,"1234567890.+-","")="",$l(NUMBER,".")<3,$l(NUMBER,"-") <3,$l(NUMBER,"+")<3,$f(NUMBER,"-")<3,$f(NUMBER,"+")<3 w "IS A NUMBER" (assuming you want any +- only at the beginning of the number) Just be cautious if you are using the 'loose' check, and then use the value for a subscript - if it doesn't pass the 'tight' check, it will be ASCII collated in the array, not numerically collated. Mark On Oct 1, 10:48*am, David Hubball <davidhubb...@talktalk.net> wrote: > Hi > > Would anyone know of a better way to check if a variable is a number. > I've written the code below which works set a variable to equal an > empty string if its not a number. > > ; Test program to check that a variable is either > ; a *whole or a decimal number. If not then set number to equal " > ; > S NUMBER="345" > > I NUMBER'?1N.N d > . I NUMBER'?1N.N1"."1N.N S NUMBER=""; *check if decimal number > . q > > W NUMBER > > Q > > Much appreciated if anyone knows of a better (or tried and tested) way > of checking if variable NUMBER is actually a number or not. > > Cheers > David |
|
|||
|
David Hubball wrote:
> > Would anyone know of a better way to check if a variable is a number. > S NUMBER="345" > > I NUMBER'?1N.N d > . I NUMBER'?1N.N1"."1N.N S NUMBER=""; check if decimal number > . q > Much appreciated if anyone knows of a better (or tried and tested) way > of checking if variable NUMBER is actually a number or not. It depends on how you define a number. Signed, unsigned, scientific, canonical... Check for unsigned integers/reals: If NUM?.n1(1"."1.n,.n) Check for signed/unsigned integers/reals If NUM?.(1"-",1"+").n1(1"."1.n,.n) Check for signed/unsigned integers/reals/scientific If NUM?.(1"-",1"+").n1(1"."1.n,.n).1(1(1"E",1"e").1(1"-",1"+")1.3n) Additionally, a If +NUM=NUM checks, if NUM is in canonical form. hth and have a nice day Julius -- (my real mail address ends with a .net tld) |
|
|||
|
On Oct 1, 6:48*pm, David Hubball <davidhubb...@talktalk.net> wrote:
> Hi > > Would anyone know of a better way to check if a variable is a number. > I've written the code below which works set a variable to equal an > empty string if its not a number. > > ; Test program to check that a variable is either > ; a *whole or a decimal number. If not then set number to equal " > ; > S NUMBER="345" > > I NUMBER'?1N.N d > . I NUMBER'?1N.N1"."1N.N S NUMBER=""; *check if decimal number > . q > > W NUMBER > > Q > > Much appreciated if anyone knows of a better (or tried and tested) way > of checking if variable NUMBER is actually a number or not. > > Cheers > David Like previous authors: w Num, " is ",$s(+Num:"",1:"not "),"number". And that is enough. If it is not number, it will not be calculated in ANY form. But if you want to check the FORM of number, look the previous examples. Worse with literals. The completely backward standard defines only USASC 7bit reactions so better to define own functions and templates so if You will check another codepage. International values with the ? operation it will have always false results. |
|
|||
|
al.veliev wrote:
> > w Num, " is ",$s(+Num:"",1:"not "),"number". > > And that is enough. If it is not number, it will not be calculated in > ANY form. Set Num=0 w Num, " is ",$s(+Num:"",1:"not "),"number". 0 is not number. Set Num="2 banana and 3 apples" w Num, " is ",$s(+Num:"",1:"not "),"number". 2 banana and 3 apples is number Anyway, I have to correct my grasp about numbers... and have a nice day Julius -- (my real mail address ends with a .net tld) |
|
|||
|
> Would anyone know of a better way to check if a variable is a number. Try: i $c(0)]]string w !,"string is either null or a number" +string=string works for most cases, but will throw errors on numeric overflow (like with string="1E999"). |
|
|||
|
On Oct 1, 11:42*pm, dahakon <n...@valid.com> wrote:
> > Would anyone know of a better way to check if a variable is a number. > > Try: > i $c(0)]]string w !,"string is either null or a number" > > +string=string works for most cases, but will throw errors on numeric > overflow (like with string="1E999"). Nice! |
|
|||
|
On 2 Oct, 06:44, rms <suba...@gmail.com> wrote:
> On Oct 1, 11:42*pm, dahakon <n...@valid.com> wrote: > > > > Would anyone know of a better way to check if a variable is a number. > > > Try: > > i $c(0)]]string w !,"string is either null or a number" > > > +string=string works for most cases, but will throw errors on numeric > > overflow (like with string="1E999"). > > Nice! I love this short solution :- i $c(0)]]string but it does not work with say -1.2 or with a minus or plus sign at the front of it. cheers David |
|
|||
|
That is cool indeed. It does work with "-1.2", but not with "+1.2".
Mark On Oct 2, 3:28*am, David Hubball <davidhubb...@talktalk.net> wrote: > On 2 Oct, 06:44, rms <suba...@gmail.com> wrote: > > > On Oct 1, 11:42*pm, dahakon <n...@valid.com> wrote: > > > > > Would anyone know of a better way to check if a variable is a number. > > > > Try: > > > i $c(0)]]string w !,"string is either null or a number" > > > > +string=string works for most cases, but will throw errors on numeric > > > overflow (like with string="1E999"). > > > Nice! > > I love this short solution :- > i $c(0)]]string > > but it does not work with say -1.2 or with a minus or plus sign at the > front of it. > > cheers > David |
|
|||
|
My testing with Cache indicates the only advantage it has over +NUMBER
is that it doesn't error with 1E99. Non-canonical numbers still say they are 'not numbers', which is technically correct, but may not be what the poster was looking for. USER>f x=1,-1.2,0,"a","","1..2","1E99","+1.2","01","1.0" w !,x,?5,$c (0)]]x 1 1 -1.2 1 0 1 a 0 1 1..2 0 1E99 0 +1.2 0 01 0 1.0 0 On Oct 2, 12:59*pm, OldMster <msi...@verizon.net> wrote: > That is cool indeed. *It does work with "-1.2", but not with "+1.2". > Mark > > On Oct 2, 3:28*am, David Hubball <davidhubb...@talktalk.net> wrote: > > > > > On 2 Oct, 06:44, rms <suba...@gmail.com> wrote: > > > > On Oct 1, 11:42*pm, dahakon <n...@valid.com> wrote: > > > > > > Would anyone know of a better way to check if a variable is a number. > > > > > Try: > > > > i $c(0)]]string w !,"string is either null or a number" > > > > > +string=string works for most cases, but will throw errors on numeric > > > > overflow (like with string="1E999"). > > > > Nice! > > > I love this short solution :- > > i $c(0)]]string > > > but it does not work with say -1.2 or with a minus or plus sign at the > > front of it. > > > cheers > > David |
|
|||
|
On Oct 2, 12:23*am, Julius Kavay
<ka...@gmx.net.valid.address.changed.to.invalid> wrote: > David Hubball wrote: > > > Would anyone know of a better way to check if a variable is a number. > > S NUMBER="345" > > > I NUMBER'?1N.N d > > . I NUMBER'?1N.N1"."1N.N S NUMBER=""; *check if decimal number > > . q > > Much appreciated if anyone knows of a better (or tried and tested) way > > of checking if variable NUMBER is actually a number or not. > > It depends on how you define a number. Signed, unsigned, scientific, > canonical... > > Check for unsigned integers/reals: > If NUM?.n1(1"."1.n,.n) > > Check for signed/unsigned integers/reals > If NUM?.(1"-",1"+").n1(1"."1.n,.n) > > Check for signed/unsigned integers/reals/scientific > If NUM?.(1"-",1"+").n1(1"."1.n,.n).1(1(1"E",1"e").1(1"-",1"+")1.3n) > > Additionally, a > If +NUM=NUM checks, if NUM is in canonical form. > > hth and > have a nice day > Julius > > -- > (my real mail address ends with a .net tld) (1234) is in accounting a form of -1234. It does not in any match and is not calculated )))) |
|
|||
|
Heh, since my degree is in accounting, I can say this - *NO*
Accounting number is a real number! :-) Mark On Oct 3, 5:56*pm, "al.veliev" <al.vel...@gmail.com> wrote: > On Oct 2, 12:23*am, Julius Kavay > > > > > > <ka...@gmx.net.valid.address.changed.to.invalid> wrote: > > David Hubball wrote: > > > > Would anyone know of a better way to check if a variable is a number. > > > S NUMBER="345" > > > > I NUMBER'?1N.N d > > > . I NUMBER'?1N.N1"."1N.N S NUMBER=""; *check if decimal number > > > . q > > > Much appreciated if anyone knows of a better (or tried and tested) way > > > of checking if variable NUMBER is actually a number or not. > > > It depends on how you define a number. Signed, unsigned, scientific, > > canonical... > > > Check for unsigned integers/reals: > > If NUM?.n1(1"."1.n,.n) > > > Check for signed/unsigned integers/reals > > If NUM?.(1"-",1"+").n1(1"."1.n,.n) > > > Check for signed/unsigned integers/reals/scientific > > If NUM?.(1"-",1"+").n1(1"."1.n,.n).1(1(1"E",1"e").1(1"-",1"+")1.3n) > > > Additionally, a > > If +NUM=NUM checks, if NUM is in canonical form. > > > hth and > > have a nice day > > Julius > > > -- > > (my real mail address ends with a .net tld) > > (1234) is in accounting a form of -1234. > It does not in any match and is not calculated )))) |
|
|||
|
On Oct 6, 6:03*am, OldMster <msi...@verizon.net> wrote:
> Accounting number is a real number! Yes, the number is real, but the result sometimes is a VERY fictitious ![]() And do not forget please that we are in different countries and our standards are DIFFERENT. And numbers are different too(I mean its show from the input stream for example). In this context I suppose that the best result would be with the user function which analyzes stream byte- by-byte and returns true or false depending of the results of comparison. In any case all this topic is a very good illustration how the cavalry jump may be a hazard for good programming. The very good topic and question indeed - thanks to David for his question and all who answered. Alexander. |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|