Normand Briere
2017-05-07 314b34423070cf127464da79a53cddf6b1c38587
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or (at
 *    your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful, but
 *    WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
/*
 *    FloatingPoint.java
 *    Copyright (C) 2002 University of Waikato, Hamilton, New Zealand
 *
 */
 
package //weka.core.
        matrix;
 
import weka.core.RevisionHandler;
import weka.core.RevisionUtils;
 
import java.text.DecimalFormat;
import java.text.FieldPosition;
 
/**
 * Class for the format of floating point numbers
 *
 * @author Yong Wang
 * @version $Revision: 1.4 $
 */
public class FloatingPointFormat
  extends DecimalFormat
  implements RevisionHandler {
 
  /** for serialization */
  private static final long serialVersionUID = 4500373755333429499L;
    
  protected DecimalFormat nf ;
  protected int width;
  protected int decimal;
  protected boolean trailing = true;
 
  /**
   * Default constructor
   */
  public FloatingPointFormat () {
    this( 8, 5 );
  }
 
  public FloatingPointFormat ( int digits ) {
    this( 8, 2 );
  }
 
  public FloatingPointFormat( int w, int d ) {
    width = w;
    decimal = d;
    nf = new DecimalFormat( pattern(w, d) );
    nf.setPositivePrefix(" ");
    nf.setNegativePrefix("-");
  }
 
  public FloatingPointFormat( int w, int d, boolean trailingZeros ) {
    this( w, d );
    this.trailing = trailingZeros;
  }
 
  public StringBuffer format(double number, StringBuffer toAppendTo, 
                FieldPosition pos) {
    StringBuffer s = new StringBuffer( nf.format(number) );
    if( s.length() > width ) {
      if( s.charAt(0) == ' ' && s.length() == width + 1 ) {
   s.deleteCharAt(0);
      }
      else {
   s.setLength( width );
   for( int i = 0; i < width; i ++ )
     s.setCharAt(i, '*');
      }
    }
    else {
      for (int i = 0; i < width - s.length(); i++)  // padding
   s.insert(0,' ');
    }
    if( !trailing && decimal > 0 ) { // delete trailing zeros
      while( s.charAt( s.length()-1 ) == '0' )
   s.deleteCharAt( s.length()-1 );
      if( s.charAt( s.length()-1 ) == '.' )
   s.deleteCharAt( s.length()-1 );
    }
   
    return toAppendTo.append( s );
  }
 
  public static String  pattern( int w, int d ) {
    StringBuffer s = new StringBuffer();      // "-##0.00"   // fw.d
    s.append( padding(w - d - 3, '#') );
    if( d == 0) s.append('0');
    else {
      s.append("0.");
      s.append( padding( d, '0') );
    }
    return s.toString();
  }
 
  private static StringBuffer  padding( int n, char c ) {
    StringBuffer text = new StringBuffer();
   
    for(int i = 0; i < n; i++ ){
      text.append( c );
    }
 
    return text;
  }
 
  public int width () {
    if( !trailing ) throw new RuntimeException( "flexible width" );
    return width;
  }
 
  /**
   * Returns the revision string.
   * 
   * @return        the revision
   */
  public String getRevision() {
    return RevisionUtils.extract("$Revision: 1.4 $");
  }
}