1 | ;------------------------------------------------------------- |
---|
2 | ;+ |
---|
3 | ; NAME: |
---|
4 | ; ISNUMBER |
---|
5 | ; PURPOSE: |
---|
6 | ; Determine if a text string is a valid number. |
---|
7 | ; CATEGORY: |
---|
8 | ; CALLING SEQUENCE: |
---|
9 | ; i = isnumber(txt, [x]) |
---|
10 | ; INPUTS: |
---|
11 | ; txt = text string to test. in |
---|
12 | ; KEYWORD PARAMETERS: |
---|
13 | ; OUTPUTS: |
---|
14 | ; x = optionaly returned numeric value if valid. out |
---|
15 | ; i = test flag: out |
---|
16 | ; 0: not a number. |
---|
17 | ; 1: txt is a long integer. |
---|
18 | ; 2: txt is a float. |
---|
19 | ; -1: first word of txt is a long integer. |
---|
20 | ; -2: first word of txt is a float. |
---|
21 | ; COMMON BLOCKS: |
---|
22 | ; NOTES: |
---|
23 | ; MODIFICATION HISTORY: |
---|
24 | ; R. Sterner. 15 Oct, 1986. |
---|
25 | ; Johns Hopkins Applied Physics Lab. |
---|
26 | ; R. Sterner, 12 Mar, 1990 --- upgraded. |
---|
27 | ; Richard Garrett, 14 June, 1992 --- fixed bug in returned float value. |
---|
28 | ; |
---|
29 | ; Copyright (C) 1986, Johns Hopkins University/Applied Physics Laboratory |
---|
30 | ; This software may be used, copied, or redistributed as long as it is not |
---|
31 | ; sold and this copyright notice is reproduced on each copy made. This |
---|
32 | ; routine is provided as is without any express or implied warranties |
---|
33 | ; whatsoever. Other limitations apply as described in the file disclaimer.txt. |
---|
34 | ;- |
---|
35 | ;------------------------------------------------------------- |
---|
36 | |
---|
37 | FUNCTION ISNUMBER, TXT0, X, help=hlp |
---|
38 | |
---|
39 | if (n_params(0) lt 1) or keyword_set(hlp) then begin |
---|
40 | print,' Determine if a text string is a valid number.' |
---|
41 | print,' i = isnumber(txt, [x])' |
---|
42 | print,' txt = text string to test. in' |
---|
43 | print,' x = optionaly returned numeric value if valid. out' |
---|
44 | print,' i = test flag: out' |
---|
45 | print,' 0: not a number.' |
---|
46 | print,' 1: txt is a long integer.' |
---|
47 | print,' 2: txt is a float.' |
---|
48 | print,' -1: first word of txt is a long integer.' |
---|
49 | print,' -2: first word of txt is a float.' |
---|
50 | return, -1 |
---|
51 | endif |
---|
52 | |
---|
53 | TXT = STRTRIM(TXT0,2) ; trim blanks. |
---|
54 | X = 0 ; define X. |
---|
55 | |
---|
56 | IF TXT EQ '' THEN RETURN, 0 ; null string not a number. |
---|
57 | |
---|
58 | SN = 1 |
---|
59 | IF NWRDS(TXT) GT 1 THEN BEGIN ; get first word if more than one. |
---|
60 | SN = -1 |
---|
61 | TXT = GETWRD(TXT,0) |
---|
62 | ENDIF |
---|
63 | |
---|
64 | f_flag = 0 ; Floating flag. |
---|
65 | b = byte(txt) |
---|
66 | w = where(b eq 43, cnt) |
---|
67 | if cnt gt 1 then return, 0 |
---|
68 | t = delchr(txt,'+') |
---|
69 | w = where(b eq 45, cnt) |
---|
70 | if cnt gt 1 then return, 0 |
---|
71 | t = delchr(t,'-') |
---|
72 | w = where(b eq 46, cnt) ; '.' |
---|
73 | if cnt gt 1 then return, 0 ; May only be 1. |
---|
74 | if cnt eq 1 then f_flag = 1 ; If one then floating. |
---|
75 | t = delchr(t,'.') |
---|
76 | w = where(b eq 101, cnt) ; 'e' |
---|
77 | if cnt gt 1 then return, 0 |
---|
78 | if cnt eq 1 then f_flag = 1 |
---|
79 | t = delchr(t,'e') |
---|
80 | w = where(b eq 69, cnt) ; 'E' |
---|
81 | if cnt gt 1 then return, 0 |
---|
82 | if cnt eq 1 then f_flag = 1 |
---|
83 | t = delchr(t,'E') |
---|
84 | w = where(b eq 100, cnt) ; 'd' |
---|
85 | if cnt gt 1 then return, 0 |
---|
86 | if cnt eq 1 then f_flag = 1 |
---|
87 | t = delchr(t,'d') |
---|
88 | w = where(b eq 68, cnt) ; 'D' |
---|
89 | if cnt gt 1 then return, 0 |
---|
90 | if cnt eq 1 then f_flag = 1 |
---|
91 | t = delchr(t,'D') |
---|
92 | if total((b eq 101)+(b eq 69)+(b eq 100)+(b eq 68)) gt 1 then return,0 |
---|
93 | b = byte(t) |
---|
94 | if total((b ge 65) and (b le 122)) ne 0 then return, 0 |
---|
95 | |
---|
96 | c = strmid(t,0,1) |
---|
97 | if (c lt '0') or (c gt '9') then return, 0 ; First char not a digit. |
---|
98 | |
---|
99 | x = txt + 0.0 ; Convert to a float. |
---|
100 | if f_flag eq 1 then return, 2*sn ; Was floating. |
---|
101 | if x eq long(x) then begin |
---|
102 | x = long(x) |
---|
103 | return, sn |
---|
104 | endif else begin |
---|
105 | return, 2*sn |
---|
106 | endelse |
---|
107 | |
---|
108 | END |
---|