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

Last change on this file since 327 was 325, checked in by pinsard, 17 years ago

modification of some headers (+some corrections) to prepare usage of the new idldoc

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