~ubuntu-branches/ubuntu/precise/weka/precise

« back to all changes in this revision

Viewing changes to weka/core/matrix/FloatingPointFormat.java

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg
  • Date: 2008-02-24 09:18:45 UTC
  • Revision ID: james.westby@ubuntu.com-20080224091845-1l8zy6fm6xipbzsr
Tags: upstream-3.5.7+tut1
ImportĀ upstreamĀ versionĀ 3.5.7+tut1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *    This program is free software; you can redistribute it and/or modify
 
3
 *    it under the terms of the GNU General Public License as published by
 
4
 *    the Free Software Foundation; either version 2 of the License, or (at
 
5
 *    your option) any later version.
 
6
 *
 
7
 *    This program is distributed in the hope that it will be useful, but
 
8
 *    WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
10
 *    General Public License for more details.
 
11
 *
 
12
 *    You should have received a copy of the GNU General Public License
 
13
 *    along with this program; if not, write to the Free Software
 
14
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
15
 
 
16
/*
 
17
 *    FloatingPoint.java
 
18
 *    Copyright (C) 2002 University of Waikato, Hamilton, New Zealand
 
19
 *
 
20
 */
 
21
 
 
22
package weka.core.matrix;
 
23
 
 
24
import java.text.DecimalFormat;
 
25
import java.text.FieldPosition;
 
26
 
 
27
/**
 
28
 * Class for the format of floating point numbers
 
29
 *
 
30
 * @author Yong Wang
 
31
 * @version $Revision: 1.3 $
 
32
 */
 
33
public class FloatingPointFormat
 
34
  extends DecimalFormat {
 
35
 
 
36
  /** for serialization */
 
37
  private static final long serialVersionUID = 4500373755333429499L;
 
38
    
 
39
  protected DecimalFormat nf ;
 
40
  protected int width;
 
41
  protected int decimal;
 
42
  protected boolean trailing = true;
 
43
 
 
44
  /**
 
45
   * Default constructor
 
46
   */
 
47
  public FloatingPointFormat () {
 
48
    this( 8, 5 );
 
49
  }
 
50
 
 
51
  public FloatingPointFormat ( int digits ) {
 
52
    this( 8, 2 );
 
53
  }
 
54
 
 
55
  public FloatingPointFormat( int w, int d ) {
 
56
    width = w;
 
57
    decimal = d;
 
58
    nf = new DecimalFormat( pattern(w, d) );
 
59
    nf.setPositivePrefix(" ");
 
60
    nf.setNegativePrefix("-");
 
61
  }
 
62
 
 
63
  public FloatingPointFormat( int w, int d, boolean trailingZeros ) {
 
64
    this( w, d );
 
65
    this.trailing = trailingZeros;
 
66
  }
 
67
 
 
68
  public StringBuffer format(double number, StringBuffer toAppendTo, 
 
69
                             FieldPosition pos) {
 
70
    StringBuffer s = new StringBuffer( nf.format(number) );
 
71
    if( s.length() > width ) {
 
72
      if( s.charAt(0) == ' ' && s.length() == width + 1 ) {
 
73
        s.deleteCharAt(0);
 
74
      }
 
75
      else {
 
76
        s.setLength( width );
 
77
        for( int i = 0; i < width; i ++ )
 
78
          s.setCharAt(i, '*');
 
79
      }
 
80
    }
 
81
    else {
 
82
      for (int i = 0; i < width - s.length(); i++)  // padding
 
83
        s.insert(0,' ');
 
84
    }
 
85
    if( !trailing && decimal > 0 ) { // delete trailing zeros
 
86
      while( s.charAt( s.length()-1 ) == '0' )
 
87
        s.deleteCharAt( s.length()-1 );
 
88
      if( s.charAt( s.length()-1 ) == '.' )
 
89
        s.deleteCharAt( s.length()-1 );
 
90
    }
 
91
        
 
92
    return toAppendTo.append( s );
 
93
  }
 
94
 
 
95
  public static String  pattern( int w, int d ) {
 
96
    StringBuffer s = new StringBuffer();      // "-##0.00"   // fw.d
 
97
    s.append( padding(w - d - 3, '#') );
 
98
    if( d == 0) s.append('0');
 
99
    else {
 
100
      s.append("0.");
 
101
      s.append( padding( d, '0') );
 
102
    }
 
103
    return s.toString();
 
104
  }
 
105
 
 
106
  private static StringBuffer  padding( int n, char c ) {
 
107
    StringBuffer text = new StringBuffer();
 
108
        
 
109
    for(int i = 0; i < n; i++ ){
 
110
      text.append( c );
 
111
    }
 
112
 
 
113
    return text;
 
114
  }
 
115
 
 
116
  public int width () {
 
117
    if( !trailing ) throw new RuntimeException( "flexible width" );
 
118
    return width;
 
119
  }
 
120
 
 
121
}