/*
 *  ===============================================================
 *  TidyTable.java: This java program demonstrates formatting and
 *  and arrangement of integer and floating-point numbers into a
 *  tidy table.  You will need these routines if you are working
 *  with Java 1.3/1.4. In Java 1.5, much of the same functionality
 *  can be achieved with the:                 
 *  
 *      System.out.printf(...)  
 *  
 *  method.  
 *  
 *  Written by: Mark Austin                           April, 2005  
 *  ===============================================================
 */

import java.lang.Math;
import java.util.*;
import java.io.*;
import java.text.*;

class TidyTable {

    /*
     *  =========================================================
     *  doubleToString(): Convert a floating point value (x) to a
     *  string of given size (w) with a specified number (n) of
     *  decimals. If the value of "w" is negative, then number is
     *  aligned to the left otherwise it is aligned on the right. 
     *  =========================================================
     */

    static final String ZEROES = "000000000000";
    static final String BLANKS = "            ";
	
    static String doubleToString( double val, int n, int w) {

       // Rounding. Add 0.5 to appropriate level of accuracy...

       if (val != 0) {
           double incr = 0.5;
           for( int j = n; j > 0; j--)
                incr /= 10; 

           val += incr;
       }

       // Construct string ....
	
       String s = Double.toString( val );
       int n1 = s.indexOf('.');
       int n2 = s.length() - n1 - 1;

       // Build rounded string ...

       if (n > n2)
           s = s + ZEROES.substring( 0, n-n2 );
       else if (n2>n)
           s = s.substring( 0, n1+n+1 );

       // Positioning characters in string of length "w" characters ...

       if( w > 0 &  w > s.length() )
           s = BLANKS.substring( 0, w-s.length() ) + s;
       else if ( w < 0 & (-w) > s.length() ) {
           w = -w;
           s = s + BLANKS.substring(0,w-s.length()) ;
       }

       return s;
    }	

    /*
     *  ==============================================================
     *  integerToString(): Convert an integer value (x) to a string of
     *  given size (w).  If the value of "w" is negative, then number
     *  is aligned to the left, otherwise it is aligned on the right. 
     *  ==============================================================
     */

    static String integerToString( int val, int w) {

       // Construct and build string ....
	
       String s = Integer.toString( val );
       s = s.substring( 0, s.length() );

       // Re-position characters in string of length "w" characters ...

       if( w > 0 &  w > s.length() )
           s = BLANKS.substring( 0, w-s.length() ) + s;
       else if ( w < 0 & (-w) > s.length() ) {
           w = -w;
           s = s + BLANKS.substring(0,w-s.length()) ;
       }

       return s;
    }	

    // =======================================================
    // Exercise doubleToString() and integerToString() methods
    // =======================================================

    public static void main(String[] args) {

        // Print header

        System.out.println("   number " + "left        " + "        right" );
        System.out.println("========= " + "============" + " ============" );

        // Test double-to-string method .....

        System.out.println("     0/7: " + doubleToString(    0.0/7, 4, -12) 
                                        + " " 
                                        + doubleToString(    0.0/7, 4,  12) );
        System.out.println("     1/7: " + doubleToString(    1.0/7, 4, -12) 
                                       + " " 
                                        + doubleToString(    1.0/7, 4,  12) );
        System.out.println("    11/7: " + doubleToString(   11.0/7, 4, -12) 
                                        + " " 
                                        + doubleToString(   11.0/7, 4,  12) );
        System.out.println("   111/7: " + doubleToString(  111.0/7, 4, -12) 
                                        + " " 
                                        + doubleToString(  111.0/7, 4,  12) );
        System.out.println("  1111/7: " + doubleToString( 1111.0/7, 4, -12) 
                                        + " " 
                                        + doubleToString( 1111.0/7, 4,  12) );
        System.out.println("-11111/7: " + doubleToString(-11111.0/7, 4, -12) 
                                        + " " 
                                        + doubleToString(-11111.0/7, 4,  12) );

        // Test integer-to-string method .....

        System.out.println("\n       0: " + integerToString(    0, -12) 
                                          + " " 
                                          + integerToString(    0,  12) );
        System.out.println("      10: " + integerToString(   10, -12) 
                                        + " " 
                                        + integerToString(   10,  12) );
        System.out.println("    1000: " + integerToString( 1000, -12) 
                                        + " " 
                                        + integerToString( 1000,  12) );
        System.out.println("-1000000: " + integerToString(-1000000, -12) 
                                        + " " 
                                        + integerToString(-1000000,  12) );

        System.out.println("========= " + "============" + " ============" );
    }
}
