import java.awt.*; import java.util.*; import java.lang.*; import java.text.*; public class NumFormat{ //Define symbolic constants for use in switch statements //(Gosling 5, 142). private static final int intType=0, doubleType=1; private static final String PlusSign="+", MinusSign="-"; private static final int Izero=0; private static final int Ione=1; // private static final int Idelta=1e-99; private static final double zero=0.0e0; private static final double one=1.0e0; private static final double ten=10.0e0; private static final double delta=1.0e-99; //Emulate some constants from Double class. private static final double MAX_VALUE=1.0e99; private static final double MIN_VALUE=-1.0e99; private static final String POSITIVE_INFINITY="Infinity"; private static final String NEGATIVE_INFINITY="-Infinity"; private static final String POSITIVE_ZERO="0"; private static final String NEGATIVE_ZERO="-0"; private String strZero=null, strLimit=null; private int Type; private int FieldLength, Ndecimals, NExpDigits, IntDigits; private Hashtable AllowedTypes, Conversion; private String strValue, SignOfNumber, strExponent=null; private int IExponent; private boolean hasExponent=false, forceExponent=false, incrementExponent=false, forceRightPadding=false; private Util util; NumFormat(int FieldLength, int Ndecimals){ setFieldLength(FieldLength); setFractionDigits(Ndecimals); setExponentDigits(2); setIntegerDigits(1); setForceExponent(false); setForceRightPadding(false); setup(); } NumFormat(){ setFieldLength(15); setFractionDigits(5); setExponentDigits(2); setIntegerDigits(1); setForceExponent(false); setForceRightPadding(false); setup(); } public void setup() { util = new Util(); AllowedTypes = new Hashtable(); AllowedTypes.put("integer", new Integer(0) ); AllowedTypes.put("double", new Integer(1) ); Conversion = new Hashtable(); Conversion.put("0", new Integer(0) ); Conversion.put("1", new Integer(1) ); Conversion.put("2", new Integer(2) ); Conversion.put("3", new Integer(3) ); Conversion.put("4", new Integer(4) ); Conversion.put("5", new Integer(5) ); Conversion.put("6", new Integer(6) ); Conversion.put("7", new Integer(7) ); Conversion.put("8", new Integer(8) ); Conversion.put("9", new Integer(9) ); } public void setType(String str) { Integer n = (Integer)AllowedTypes.get(str); Type = n.intValue(); } public int getType() { return(Type); } public void setForceExponent(boolean forceExp) { forceExponent = forceExp; } public void setFieldLength(int value) { FieldLength = value; } public void setFractionDigits(int value) { Ndecimals = value; } public void setIntegerDigits(int value) { IntDigits = value; } public void setExponentDigits(int value) { NExpDigits = value; } public void setForceRightPadding(boolean forceRightPadding) { this.forceRightPadding = forceRightPadding; } //Return an integer representation preceded by a maximum of //IntDigits '0's and a total lenght of FieldLength. public String format(int value) { String theSign = Sign(value); Integer Ivalue = new Integer(value); String S1 = Ivalue.toString(); String S2 = stripSign(S1); S1 = LeftPad(S2, '0', IntDigits); if(theSign.compareTo(MinusSign) == 0){ S2 = addSign(S1, theSign); }else{ S2 = S1; } S1 = LeftPad(S2, ' ', FieldLength); return(S1); } public String format(double value) { String strNum=null; if(isZero(value))return(strZero); if(isBigOrSmall(value))return(strLimit); SignOfNumber = Sign(value); if(!forceExponent)strNum = numField(value); if( forceExponent)strNum = expoField(value); return(strNum); } //Return a floating-point representation occupying a field length //of FieldLength, forcing inclusion of the exponential part. public String expoField(double value) { double Value, val; String S1, S2; int power; hasExponent = true; Value = Math.abs(value); power = util.Ilog10(Value); if(power == Izero){ val = Value; }else{ val = Value / Math.pow(ten, (double)power); } //This condition insures that the floating-point representation //(preceding the exponent) will begin with "digit.mmmmm..." //with digit != 0 when abs(value) < 1. if(Value = zero){ theSign = PlusSign; }else{ theSign = MinusSign; } return(theSign); } //Return the sign of an integer value, as a string. public String Sign(int value) { String theSign; if(value >= Izero){ theSign = PlusSign; }else{ theSign = MinusSign; } return(theSign); } //Assess if a value can be equated to +-Infinity. public boolean isBigOrSmall(double value) { String string; if(value < MIN_VALUE || value > MAX_VALUE){ if(value = IField){ newStr = string; return(newStr); } delta = IField - Len; CharStr = new char[delta]; for(i=0; i= Ipad)return(str1); n = Ipad - NdecimalPoints; //Number of 0's to fill in CharStr = new char[n]; for(i=0; i= 5 && I != 9){ I++; //I is the rounded digit. newStr = String.copyValueOf(CharStr, 0, newLen); newCharStr = newStr.toCharArray(); char ch = DigitToChar(I); newCharStr[theIndex] = ch; //Insert the rounded digit. newStr = String.copyValueOf(newCharStr); return(newStr); } //Truncation with rounding-off of //additional significant digits. //Precede representation by a zero in case rounding affects //digits to the left of the decimal point. string = strZero.concat(str); dotIndex++; Len++; CharStr = string.toCharArray(); //Split the fields left and right of the decimal point. Left = String.copyValueOf(CharStr, 0, dotIndex); Right = String.copyValueOf(CharStr, dotIndex+1, Len-dotIndex-1); //Now join these fields so that string refers to the //representation stripped of the decimal point. This //relieves us of the burden of taking into account the //position of the decimal point in what follows. string = Left.concat(Right); Len--; //The length of the (stripped) string; theIndex = dotIndex - 1 + Ipad; //The index of the last //significant digit of string. newLen = theIndex + 1; //Length of the rounded string. //First, copy original (stripped) string to newStr CharStr = string.toCharArray(); newStr = String.copyValueOf(CharStr, 0, newLen); //Next, substitute appropriate "numbers" in the character //representation of newStr. newCharStr = newStr.toCharArray(); newCharStr[theIndex] = DigitToChar(0); for(i=theIndex-1; i>=0; i--){ I = CharToDigit(CharStr[i]); if(I < 9){ newCharStr[i] = DigitToChar(++I); break; } newCharStr[i] = DigitToChar(0); } //When i=0, we've substituted the added leading '0' by a '1'. //If the representation contains an exponent, we will have //to shift the decimal point one position to the left and also //increment the value of the exponent by 1. if(i == 0){ Istart = 0; if(hasExponent)incrementExponent = true; }else{ Istart = 1; } //Split the resulting rounded "number" in 2 fields: one //for the field to the left of the decimal point; the other //for the field to the right of the decimal point. // //Round-off has produced a first digit '1' where we previously //had placed a '0'. If the representation contains an exponent, //we shift the decimal point one position to the left. Following //this we will have to increment the value of the exponent by 1. if(incrementExponent){ Left = String.copyValueOf(newCharStr, 0, dotIndex-1); Right = String.copyValueOf(newCharStr, dotIndex-1, Ipad); }else{ Left = String.copyValueOf(newCharStr, Istart, dotIndex-Istart); Right = String.copyValueOf(newCharStr, dotIndex, Ipad); } //Now add the decimal point and return resulting rounded //floating-point representation. str = Left.concat(strDot); newStr = str.concat(Right); return(newStr); } //Convert a character - representing an integer - to the //corresponding integer value. public int CharToDigit(char value) { String str = CharToString(value); Integer n = (Integer)Conversion.get(str); return(n.intValue()); } //Convert a character to a string. public String CharToString(char value){ String string; char Charvec[]; Charvec = new char[1]; Charvec[0] = value; string = String.copyValueOf(Charvec); return(string); } //Convert an integer value to it's corresponding //character representation. public char DigitToChar(int value){ Integer Ivalue; String str; char CharStr[]; Ivalue = new Integer(value); str = Ivalue.toString(); CharStr = str.toCharArray(); return(CharStr[0]); } //Convert a string to it's corresponding integer value. public int StringToInt(String string) { String sgn = getSign(string); String str = stripSign(string); Integer Ivalue = new Integer(str); int value = Ivalue.intValue(); //Restore the sign. if(sgn != null){ if(sgn.compareTo(MinusSign) == 0)value = -value; } return(value); } }