FlexibleDecimalFormat.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 5k
Category:

Windows Develop

Development Platform:

Java

  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.  *    FlexibleDecimalFormat.java
  17.  *    Copyright (C) 2002 Yong Wang
  18.  *
  19.  */
  20. package weka.classifiers.functions.pace;
  21. import java.text.DecimalFormat;
  22. import java.text.DecimalFormatSymbols;
  23. import java.text.FieldPosition;
  24. public class FlexibleDecimalFormat extends DecimalFormat {
  25.   private DecimalFormat nf = null;
  26.   private int      digits = 7;
  27.   private boolean  exp = false; 
  28.   private int      intDigits = 1;
  29.   private int      decimalDigits = 0;
  30.   private int      expIntDigits = 1;          // ??? 
  31.   private int      expDecimalDigits = 0;      // ???
  32.   private int      power = 2;
  33.   private boolean  trailing = false;
  34.   private boolean  grouping = false;
  35.   private boolean  sign = false;
  36.     
  37.   public FlexibleDecimalFormat ( ) {
  38.     this( 5 );
  39.   }
  40.   public FlexibleDecimalFormat ( int digits ) {
  41.     if( digits < 1 ) 
  42.       throw new IllegalArgumentException("digits < 1");
  43.     this.digits = digits;
  44.     intDigits = 1;
  45.   }
  46.   public FlexibleDecimalFormat ( int digits, boolean trailing ) {
  47.     this( digits );
  48.     this.trailing = trailing;
  49.   }
  50.   public FlexibleDecimalFormat ( int digits, boolean exp, boolean trailing, 
  51.  boolean grouping ) {
  52.     this.trailing = trailing;
  53.     this.exp = exp;
  54.     this.digits = digits;
  55.     this.grouping = grouping;
  56.     if( exp ) {
  57.       this.intDigits = 1;
  58.       this.decimalDigits = digits - intDigits;
  59.     }
  60.     else {
  61.       this.decimalDigits = decimalDigits;
  62.       this.intDigits = Math.max( 1, digits - decimalDigits );
  63.     }
  64.   }
  65.   public FlexibleDecimalFormat ( double d ) {
  66.     newFormat( d );
  67.   }
  68.   private void newFormat ( double d ) {
  69.     if( needExponentialFormat( d ) ) {
  70.       exp = true;
  71.       intDigits = 1;
  72.       expDecimalDigits = decimalDigits( d, true );
  73.       if( d < 0) sign = true;
  74.       else sign = false;
  75.     }
  76.     else {
  77.       exp = false;
  78.       intDigits = Math.max(1, intDigits( d ));
  79.       decimalDigits = decimalDigits( d, false );
  80.       if( d < 0.0 ) sign = true;
  81.       else sign = false;
  82.     }
  83.   }
  84.   public void update ( double d ) {
  85.     if( Math.abs( intDigits(d) -1 ) > 99 ) power = 3;
  86.     expIntDigits = 1;
  87.     expDecimalDigits = Math.max( expDecimalDigits, 
  88.  decimalDigits( d, true ));
  89.     if( d < 0) sign = true;
  90.     if( needExponentialFormat( d ) || exp ) {
  91.       exp = true;
  92.     }
  93.     else {
  94.       intDigits = Math.max(intDigits, intDigits( d ));
  95.       decimalDigits = Math.max(decimalDigits, decimalDigits( d, false ));
  96.       if( d < 0) sign = true;
  97.     }
  98.   }
  99.   private static int intDigits ( double d ) {
  100.     return (int) Math.floor( Math.log( Math.abs( d * (1 + 1e-14) ) ) / 
  101.      Math.log ( 10 ) ) + 1;
  102.   }
  103.     
  104.   private int decimalDigits ( double d, boolean expo ) {
  105.     if( d == 0.0 ) return 0;
  106.     d = Math.abs( d );
  107.     int e = intDigits( d );
  108.     if( expo ) {
  109.       d /= Math.pow(10, e-1);
  110.       e = 1;
  111.     }
  112.     if( e >= digits ) return 0;
  113.     int iD = Math.max(1, e);  
  114.     int dD = digits - e;
  115.     if( !trailing && dD > 0 ) {  // to get rid of trailing zeros
  116.       FloatingPointFormat f = new 
  117. FloatingPointFormat( iD + 1 + dD, dD, true);
  118.       String dString = f.format( d );
  119.       while( dD > 0 ) {
  120. if( dString.charAt(iD+1+dD-1) == '0' ) {
  121.   dD--;
  122. }
  123. else break;
  124.       }
  125.     }
  126.     return dD;
  127.   }
  128.     
  129.   public boolean  needExponentialFormat ( double d ) {
  130.     if( d == 0.0 ) return false;
  131.     int e = intDigits( d );
  132.     if( e > digits + 5 || e < -3 ) return true;
  133.     else return false;
  134.   }
  135.     
  136.   public void grouping ( boolean grouping ) {
  137.     this.grouping = grouping;
  138.   }
  139.     
  140.   private static  void println ( Object obj ){
  141.     System.out.println( obj );
  142.   }
  143.   private void setFormat ( ) {
  144.     int dot = 1;
  145.     if( decimalDigits == 0) dot = 0;
  146.     if( exp ) 
  147.       nf = new ExponentialFormat( 1 + expDecimalDigits, power, sign, 
  148.   grouping || trailing );
  149.     else { 
  150.       int s = sign ? 1 : 0;
  151.       nf = new FloatingPointFormat( s +intDigits +dot +decimalDigits, 
  152.     decimalDigits, grouping || trailing);
  153.     }
  154.   }
  155.     
  156.   private void setFormat ( double d ) {
  157.     newFormat( d );
  158.     setFormat();
  159.   }
  160.   public StringBuffer format (double number, StringBuffer toAppendTo,
  161.       FieldPosition pos) {
  162.     if( grouping ) {
  163.       if( nf == null ) {
  164. setFormat();
  165.       }
  166.     }
  167.     else setFormat( number );
  168.     return toAppendTo.append( nf.format(number) );
  169.   }
  170.   public int width () {
  171.     if( !trailing && !grouping ) 
  172.       throw new RuntimeException( "flexible width" );
  173.     return format(0.).length();
  174.   }
  175.     
  176.   public StringBuffer formatString ( String str ) {
  177.     int w = width();
  178.     int h = ( w - str.length() ) / 2;
  179.     StringBuffer text = new StringBuffer();
  180.     for(int i = 0; i < h; i++ ){
  181.       text.append( ' ' );
  182.     }
  183.     text.append( str );
  184.     for(int i = 0; i < w - h - str.length(); i++ ){
  185.       text.append( ' ' );
  186.     }
  187.     return text;
  188.   }
  189. }