| 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 weka.core.RevisionHandler; |
|---|
| 25 | import weka.core.RevisionUtils; |
|---|
| 26 | |
|---|
| 27 | import java.text.DecimalFormat; |
|---|
| 28 | import java.text.FieldPosition; |
|---|
| 29 | |
|---|
| 30 | /** |
|---|
| 31 | * Class for the format of floating point numbers |
|---|
| 32 | * |
|---|
| 33 | * @author Yong Wang |
|---|
| 34 | * @version $Revision: 5953 $ |
|---|
| 35 | */ |
|---|
| 36 | public class FloatingPointFormat |
|---|
| 37 | extends DecimalFormat |
|---|
| 38 | implements RevisionHandler { |
|---|
| 39 | |
|---|
| 40 | /** for serialization */ |
|---|
| 41 | private static final long serialVersionUID = 4500373755333429499L; |
|---|
| 42 | |
|---|
| 43 | protected DecimalFormat nf ; |
|---|
| 44 | protected int width; |
|---|
| 45 | protected int decimal; |
|---|
| 46 | protected boolean trailing = true; |
|---|
| 47 | |
|---|
| 48 | /** |
|---|
| 49 | * Default constructor |
|---|
| 50 | */ |
|---|
| 51 | public FloatingPointFormat () { |
|---|
| 52 | this( 8, 5 ); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | public FloatingPointFormat ( int digits ) { |
|---|
| 56 | this( 8, 2 ); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | public FloatingPointFormat( int w, int d ) { |
|---|
| 60 | width = w; |
|---|
| 61 | decimal = d; |
|---|
| 62 | nf = new DecimalFormat( pattern(w, d) ); |
|---|
| 63 | nf.setPositivePrefix(" "); |
|---|
| 64 | nf.setNegativePrefix("-"); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | public FloatingPointFormat( int w, int d, boolean trailingZeros ) { |
|---|
| 68 | this( w, d ); |
|---|
| 69 | this.trailing = trailingZeros; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | public StringBuffer format(double number, StringBuffer toAppendTo, |
|---|
| 73 | FieldPosition pos) { |
|---|
| 74 | StringBuffer s = new StringBuffer( nf.format(number) ); |
|---|
| 75 | if( s.length() > width ) { |
|---|
| 76 | if( s.charAt(0) == ' ' && s.length() == width + 1 ) { |
|---|
| 77 | s.deleteCharAt(0); |
|---|
| 78 | } |
|---|
| 79 | else { |
|---|
| 80 | s.setLength( width ); |
|---|
| 81 | for( int i = 0; i < width; i ++ ) |
|---|
| 82 | s.setCharAt(i, '*'); |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | else { |
|---|
| 86 | for (int i = 0; i < width - s.length(); i++) // padding |
|---|
| 87 | s.insert(0,' '); |
|---|
| 88 | } |
|---|
| 89 | if( !trailing && decimal > 0 ) { // delete trailing zeros |
|---|
| 90 | while( s.charAt( s.length()-1 ) == '0' ) |
|---|
| 91 | s.deleteCharAt( s.length()-1 ); |
|---|
| 92 | if( s.charAt( s.length()-1 ) == '.' ) |
|---|
| 93 | s.deleteCharAt( s.length()-1 ); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | return toAppendTo.append( s ); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | public static String pattern( int w, int d ) { |
|---|
| 100 | StringBuffer s = new StringBuffer(); // "-##0.00" // fw.d |
|---|
| 101 | s.append( padding(w - d - 3, '#') ); |
|---|
| 102 | if( d == 0) s.append('0'); |
|---|
| 103 | else { |
|---|
| 104 | s.append("0."); |
|---|
| 105 | s.append( padding( d, '0') ); |
|---|
| 106 | } |
|---|
| 107 | return s.toString(); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | private static StringBuffer padding( int n, char c ) { |
|---|
| 111 | StringBuffer text = new StringBuffer(); |
|---|
| 112 | |
|---|
| 113 | for(int i = 0; i < n; i++ ){ |
|---|
| 114 | text.append( c ); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | return text; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | public int width () { |
|---|
| 121 | if( !trailing ) throw new RuntimeException( "flexible width" ); |
|---|
| 122 | return width; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | /** |
|---|
| 126 | * Returns the revision string. |
|---|
| 127 | * |
|---|
| 128 | * @return the revision |
|---|
| 129 | */ |
|---|
| 130 | public String getRevision() { |
|---|
| 131 | return RevisionUtils.extract("$Revision: 5953 $"); |
|---|
| 132 | } |
|---|
| 133 | } |
|---|