Normand Briere
2018-07-07 09ddd38fd4a8a7100c834a5e976f4796fae53541
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
 * 
 */
package timeflow.format.field;
 
public class FormatDouble extends FieldFormat
{    
   @Override
   public String format(Object o) {
       return o.toString();
   }
 
   @Override
   public Object _parse(String s) {
       if (s==null || s.trim().length()==0)
           return null;
       return parseDouble(s);
   }
   
   public static double parseDouble(String s)
   {
       int n=s.length();
       if (n>3)
       {
           if (s.charAt(0)=='(' && s.charAt(n-1)==')')
           {
               s='-'+s.substring(1,n-1);
               n--;
           }
       }
       // remove $,%, etc.
       StringBuffer b=new StringBuffer();
       for (int i=0; i<n; i++)
       {
           char c=s.charAt(i);
           if (Character.isDigit(c) || c=='-' || c=='.')
               b.append(c);
       }
       
       try
       {
       return Double.parseDouble(b.toString());
       }
       catch (RuntimeException e)
       {
           System.out.println("b="+b);
           throw e;
       }
   }
 
 
   @Override
   public Class getType() {
       return Double.class;
   }    
   @Override
   public double scoreFormatMatch(String s) {
       s=s.trim();
       int n=s.length();
       if (n==5) // possible zip code
       {
           if (s.charAt(0)=='0')
               return -3; // gotta be a zip code!
           return 0;
       }
       if (n==9) // possible zip+4, but really, who knows...
           return 0;
       
       if (n==4) // possible date.
       {
           try
           {
               int x=Integer.parseInt(s);
               if (x>1900 && x<2030)
                   return -1; // that's very likely a date.
               if (x>1700 && x<2100)
                   return 0; // you don't know.
               
           }
           catch (Exception e) {} // evidently not a date :-)
       }
       
       if (n==0)
           return -.1;
       int ok=0;
       int bad=0;
       for (int i=0; i<n; i++)
       {
           char c=s.charAt(i);
           if (Character.isDigit(c) || c=='.' || c==',' || c=='-' || c=='$' || c=='%')
               ok++;
           else
               bad++;
       }
       return 4-5*bad;
   }
 
   @Override
   public String getHumanName() {
       return "Number";
   }
 
}