/*
 * MyNumberFormat.java
 * Assignment #3, Question #1
 * CS 4762, Winter 2003
 *
 *    Written by Todd Wareham, mid Mar, 2003
 *
 */
 
import java.text.DecimalFormat;



/*
 *  Class MyNumberFormat
 *   Contains various number-formatting routines.
 */

class MyNumberFormat {

/*
 *  Returns a string of length FLEN in which integer X is 
 *   right-justified by blanks if X has < FLEN digits; otherwise,
 *   returns the string representation of X.
 */

    public static String format(int x, int flen){
	String s = Integer.toString(x);
	int nb = flen - s.length();
	for (int i = 1; i <= nb; i++)
	    s = " " + s;
	return(s);
    }  // End of method format (int)


/*
 *  Returns a string of length FLEN in which double X (relative to
 *   decimal print-pattern dd.d) is right-justified by blanks in a
 *   field of length FLEN characters.
 */

    public static String format(double x, int flen){
	DecimalFormat fmt = new DecimalFormat("0.0");
	String s = fmt.format(x);
	int nb = flen - s.length();
	for (int i = 1; i <= nb; i++)
	    s = " " + s;
	return(s);
    }  // End of method format (double)


/*
 *  Returns a string consisting of string X right-justified by
 *   blanbks in a field of length FLEN characters if x.length < FLEN,
 *   and string x otherwise.
 */

    public static String rightJustify(String x, int flen){
	String s = x;
	int nb = flen - s.length();
	for (int i = 1; i <= nb; i++)
	    s = " " + s;
	return(s);
    }  // End of method rightJustify


/*
 *  Returns a string consisting of string X left-justified by
 *   blanbks in a field of length FLEN characters if x.length < FLEN,
 *   and string x otherwise.
 */

    public static String leftJustify(String x, int flen){
	String s = x;
	int nb = flen - s.length();
	for (int i = 1; i <= nb; i++)
	    s = s + " ";
	return(s);
    }  // End of method leftJustify

}  // End of class MyNumberFormat
