source: trunk/SRC/ToBeReviewed/STRING/isnumber.pro @ 134

Last change on this file since 134 was 134, checked in by navarro, 18 years ago

change *.pro file properties (del eof-style, del executable, set keywords Id

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.1 KB
Line 
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;       R. Sterner, 1999 Nov 30 --- Fixed a bug found by Kristian Kjaer, Denmark
29;
30; Copyright (C) 1986, Johns Hopkins University/Applied Physics Laboratory
31; This software may be used, copied, or redistributed as long as it is not
32; sold and this copyright notice is reproduced on each copy made.  This
33; routine is provided as is without any express or implied warranties
34; whatsoever.  Other limitations apply as described in the file disclaimer.txt.
35;-
36;-------------------------------------------------------------
37 
38        function isnumber, txt0, x, help=hlp
39;
40  compile_opt idl2, strictarrsubs
41;
42        if (n_params(0) lt 1) or keyword_set(hlp) then begin
43          print,' Determine if a text string is a valid number.'
44          print,' i = isnumber(txt, [x])'
45          print,'   txt = text string to test.                      in'
46          print,'   x = optionaly returned numeric value if valid.  out'
47          print,'   i = test flag:                                  out'
48          print,'       0: not a number.'
49          print,'       1: txt is a long integer.'
50          print,'       2: txt is a float.'
51          print,'       -1: first word of txt is a long integer.'
52          print,'       -2: first word of txt is a float.'
53          return, -1
54        endif
55 
56        txt = strtrim(txt0,2)   ; trim blanks.
57        x = 0                   ; define X.
58 
59        if txt eq '' then return, 0     ; null string not a number.
60 
61        sn = 1
62        if nwrds(txt) gt 1 then begin   ; get first word if more than one.
63          sn = -1
64          txt = getwrd(txt,0)
65        endif
66         
67        f_flag = 0                      ; Floating flag.
68        b = byte(txt)                   ; Convert to byte array.
69        if b[0] eq 45 then b=b[1:*]     ; Drop leading '-'.   ; Kristian Kjaer
70        if b[0] eq 43 then b=b[1:*]     ; Drop leading '+'.   ; bug fix.
71        w = where(b eq 43, cnt)         ; Look for '+'
72        if cnt gt 1 then return, 0      ; Alow only 1.
73        t = delchr(txt,'+')             ; Drop it.
74        w = where(b eq 45, cnt)         ; Look for '-'
75        if cnt gt 1 then return, 0      ; Allow only 1.
76        t = delchr(t,'-')               ; Drop it.
77        w = where(b eq 46, cnt)         ; Look for '.'
78        if cnt gt 1 then return, 0      ; Allow only 1.
79        if cnt eq 1 then f_flag = 1     ; If one then floating.
80        t = delchr(t,'.')               ; Drop it.
81        w = where(b eq 101, cnt)        ; Look for 'e'
82        if cnt gt 1 then return, 0      ; Allow only 1.
83        if cnt eq 1 then f_flag = 1     ; If 1 then assume float.
84        t = delchr(t,'e')               ; Drop it.
85        w = where(b eq 69, cnt)         ; Look for 'E'
86        if cnt gt 1 then return, 0      ; Allow only 1.
87        if cnt eq 1 then f_flag = 1     ; If 1 then assume float.
88        t = delchr(t,'E')               ; Drop it.
89        w = where(b eq 100, cnt)        ; Look for 'd'
90        if cnt gt 1 then return, 0      ; Allow only 1.
91        if cnt eq 1 then f_flag = 1     ; If 1 then assume float.
92        t = delchr(t,'d')               ; Drop it.
93        w = where(b eq 68, cnt)         ; Look for 'D'
94        if cnt gt 1 then return, 0      ; Allow only 1.
95        if cnt eq 1 then f_flag = 1     ; If 1 then assume float.
96        t = delchr(t,'D')               ; Drop it.
97        ;-----  Allow only one 'e', 'E', 'd', or 'D'  --------
98        if total((b eq 101)+(b eq 69)+(b eq 100)+(b eq 68)) gt 1 then return,0
99        b = byte(t)
100        ;-----  Allow no alphabetic characters  -----------
101        if total((b ge 65) and (b le 122)) ne 0 then return, 0
102 
103        c = strmid(t,0,1)
104        if (c lt '0') or (c gt '9') then return, 0  ; First char not a digit.
105 
106        x = txt + 0.0                               ; Convert to a float.
107        if f_flag eq 1 then return, 2*sn            ; Was floating.
108        if x eq long(x) then begin
109          x = long(x)
110          return, sn
111        endif else begin
112          return, 2*sn
113        endelse
114 
115        end
Note: See TracBrowser for help on using the repository browser.