sharkysoft.com

com.sharkysoft.printf.engine
Class RealFormatter

java.lang.Object
  extended bycom.sharkysoft.printf.engine.StringFormatter
      extended bycom.sharkysoft.printf.engine.NumberFormatter
          extended bycom.sharkysoft.printf.engine.RealFormatter

public class RealFormatter
extends NumberFormatter

Formats real numbers.

Details: A RealFormatter formats numbers according to criteria selected by the user. The user can control various formatting parameters, such as field width, justification style (left-align, right-align, center, etc.), precision, etc.

Since applications that format real numbers frequently format more than one real number with the same style, this class is designed to "remember" the currently selected formatting parameters so that they can be reused with each format call. This approach reduces the amount of parameters which must be passed during the format calls and results in more efficient execution.

Example: Output the contents of a float array called vals, one per line, right-justified in a 25-character-wide field. Round each float to two decimal places, but always show the two decimal places even if they are zeros.

float[] vals = {1.0F, (float) Math.PI, -22};
RealFormatter formatter = new RealFormatter();
formatter.setFieldWidth(25);
formatter.setPadChar(':'); // non-space chosen for emphasis
formatter.setAlignment(AlignmentMode.gpRight);
formatter.setMinRightDigits(2);
formatter.setMaxRightDigits(2);
for (int i = 0; i < vals.length; ++ i)
  System.out.println(formatter.format(vals[i]));

This produces the following output:

:::::::::::::::::::::1.00
:::::::::::::::::::::3.14
:::::::::::::::::::-22.00

Formatting parameters can be updated at any time, and doing so will change the way text is formatted afterwards. Methods in this class only support formatting for radix=10. For a detailed description of all user-controllable parameters, see the documentation below and in the superclass.


Field Summary
protected  char mcLeftPadDigit
          Left pad digit.
protected  char mcRightPadDigit
          Right pad digit.
protected  int mnMaxLeftDigits
          Maximum digits left of decimal point.
protected  int mnMaxRightDigits
          Maximum digits right of decimal point.
protected  int mnMinLeftDigits
          Minimum digits left of decimal point.
protected  int mnMinRightDigits
          Minimum digits right of the decimal point.
protected  IntegerFormatter mpExponentFormatter
          Exponent mpFormatter for scientific notation.
protected  boolean mzShowDecPoint
          Show decimal point when optional.
 
Fields inherited from class com.sharkysoft.printf.engine.NumberFormatter
mnRadix, mnSigDigits, msNegPrefix, msNegSuffix, msPosPrefix, msPosSuffix, msZeroPrefix, msZeroSuffix, mzUpperCase
 
Fields inherited from class com.sharkysoft.printf.engine.StringFormatter
mcPadChar, mnAlignment, mnCropping, mnFieldWidth
 
Constructor Summary
RealFormatter()
          Default constructor.
 
Method Summary
 java.lang.String format(java.math.BigDecimal ipValue)
          Formats real value.
 java.lang.String format(double idValue)
          Formats real value.
 java.lang.String format(float ifValue)
          Formats real value.
 java.lang.String format(com.sharkysoft.math.MantissaExponent ipValue)
          Formats real value.
 IntegerFormatter getExponentFormatter()
          Retrieves ExponentFormatter property.
 char getLeftPadDigit()
          Retrieves LeftPadDigit property.
 int getMaxLeftDigits()
          Retrieves MaxLeftDigits property.
 int getMaxRightDigits()
          Retrieves MaxRightDigits property.
 int getMinLeftDigits()
          Retrieves MinLeftDigits property.
 int getMinRightDigits()
          Retrieves MinRightDigits property.
 char getRightPadDigit()
          Retrieves RightPadDigit property.
 boolean getShowDecPoint()
          Retrieves ShowDecPoint property.
 void setLeftPadDigit(char icLeftPadDigit)
          Updates LeftPadDigit property.
 void setMaxLeftDigits(int inMaxLeftDigits)
          Updates MaxLeftDigits property.
 void setMaxRightDigits(int inMaxRightDigits)
          Updates MaxRightDigits property.
 void setMinLeftDigits(int inMinLeftDigits)
          Updates MinLeftDigits property.
 void setMinRightDigits(int inMinRightDigits)
          Updates MinRightDigits property.
 void setRightPadDigit(char icRightPadDigit)
          Updates RightPadDigit property.
 void setShowDecPoint(boolean izShowDecPoint)
          Updates ShowDecPoint property.
 
Methods inherited from class com.sharkysoft.printf.engine.NumberFormatter
getNegPrefix, getNegSuffix, getPosPrefix, getPosSuffix, getRadix, getSigDigits, getUpperCase, getZeroPrefix, getZeroSuffix, setNegPrefix, setNegSuffix, setPosPrefix, setPosSuffix, setRadix, setSigDigits, setUpperCase, setZeroPrefix, setZeroSuffix
 
Methods inherited from class com.sharkysoft.printf.engine.StringFormatter
format, format, getAlignment, getCropping, getFieldWidth, getPadChar, setAlignment, setCropping, setFieldWidth, setPadChar, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

mpExponentFormatter

protected final IntegerFormatter mpExponentFormatter
Exponent mpFormatter for scientific notation.

Details: Property ExponentFormatter is the configurable IntegerFormatter used to render exponent integers when real values are rendered in scientific notation.

Initial configuration: MinDigits=2, ZeroPrefix="E", PosPrefix="E+", NegPrefix="E-".

See Also:
getExponentFormatter()

mnMaxLeftDigits

protected int mnMaxLeftDigits
Maximum digits left of decimal point.

Details: Property MaxLeftDigits is the maximum number of digits to render left of the decimal point. The value will wrap like an odometer if it is too large. Default value: Integer.MAX_VALUE.

See Also:
getMaxLeftDigits(), setMaxLeftDigits(int)

mnMinLeftDigits

protected int mnMinLeftDigits
Minimum digits left of decimal point.

Details: Property MinLeftDigits is the minimum number of digits to render left of the decimal point. If the value isn't large enough, the value will be padded with the left pad digit (LeftPadDigit). Default value: 1.

See Also:
getMinLeftDigits(), setMinLeftDigits(int)

mcLeftPadDigit

protected char mcLeftPadDigit
Left pad digit.

Details: Property LeftPadDigit determines the digit or character to use for left-padding numbers. This character will be used only when the value of MinLeftDigits requires it. If this property is set to 0, the space character will be used, but the prefix string will appear between the pad digits and the actual number. Otherwise, the padding will appear between the prefix string and the number. Default value: '0'.

See Also:
getLeftPadDigit(), setLeftPadDigit(char)

mzShowDecPoint

protected boolean mzShowDecPoint
Show decimal point when optional.

Details: Property ShowDecPoint determines whether or not the decimal point will be included when it is possible render the number without it. Default value: false.

See Also:
getShowDecPoint(), setShowDecPoint(boolean)

mnMinRightDigits

protected int mnMinRightDigits
Minimum digits right of the decimal point.

Details: Property MinRightDigits determines the minimum number of digits that will be rendered right of the decimal point. If the fractional portion of the number isn't long enough to fill out the minimum number of digits, the extra places will be set to RightPadDigit. Default value: 0.

See Also:
getMinRightDigits(), setMinRightDigits(int)

mcRightPadDigit

protected char mcRightPadDigit
Right pad digit.

Details: Property RightPadDigit determines the digit or character to use for right-padding numbers. This character will be used only when the value of MinRightDigits requires it. If this property is set to 0, the space character will be used, but the suffix string will appear between the pad digits and the actual number. Otherwise, the padding will appear between the suffix string and the number. Default value: '0'.

See Also:
getRightPadDigit(), setRightPadDigit(char)

mnMaxRightDigits

protected int mnMaxRightDigits
Maximum digits right of decimal point.

Details: Property MaxRightDigits determines the maximum number of digits to show on the right of the decimal point. If the fractional portion of the number is "longer" than this number of places, the value will be rounded. If, for a particular value being rendered, the configuration of MaxRightDigits and SigDigits is incompatible, the rendered value will rounded to the appropriate number of significant digits, and then the trailing zeros will be trimmed, if there are any, until the MaxRightDigits constraint is satisfied, or until there are no more trailing zeros to trim. Default value: Integer.MAX_VALUE.

See Also:
getMaxRightDigits(), setMaxRightDigits(int)
Constructor Detail

RealFormatter

public RealFormatter()
Default constructor.

Details: This constructor initializes a new instance with the default value for each property, as specified in the property documentation.

Method Detail

getExponentFormatter

public IntegerFormatter getExponentFormatter()
Retrieves ExponentFormatter property.

Returns:
current value
See Also:
mpExponentFormatter

getMaxLeftDigits

public int getMaxLeftDigits()
Retrieves MaxLeftDigits property.

Returns:
current value
See Also:
mnMaxLeftDigits

setMaxLeftDigits

public void setMaxLeftDigits(int inMaxLeftDigits)
Updates MaxLeftDigits property.

Details: This setter updates the MaxLeftDigits property. The new value may not be smaller than zero. If the new value is smaller than MinLeftDigits, then MinLeftDigits will be set to this new value as well.

Parameters:
inMaxLeftDigits - new value
See Also:
mnMaxLeftDigits

getMinLeftDigits

public int getMinLeftDigits()
Retrieves MinLeftDigits property.

Returns:
current value
See Also:
mnMinLeftDigits

setMinLeftDigits

public void setMinLeftDigits(int inMinLeftDigits)
Updates MinLeftDigits property.

Details: This setter updates the MinLeftDigits property. The new value may not be smaller than zero. If the new value is larger than MaxLeftDigits, then MaxLeftDigits will be set to the new value as well.

Parameters:
inMinLeftDigits - new value
See Also:
mnMinLeftDigits

getLeftPadDigit

public char getLeftPadDigit()
Retrieves LeftPadDigit property.

Returns:
current value
See Also:
mcLeftPadDigit

setLeftPadDigit

public void setLeftPadDigit(char icLeftPadDigit)
Updates LeftPadDigit property.

Parameters:
icLeftPadDigit - new value
See Also:
mcLeftPadDigit

getShowDecPoint

public boolean getShowDecPoint()
Retrieves ShowDecPoint property.

Returns:
current value
See Also:
mzShowDecPoint

setShowDecPoint

public void setShowDecPoint(boolean izShowDecPoint)
Updates ShowDecPoint property.

Parameters:
izShowDecPoint -
See Also:
mzShowDecPoint

getMinRightDigits

public int getMinRightDigits()
Retrieves MinRightDigits property.

Returns:
current value
See Also:
mnMinRightDigits

setMinRightDigits

public void setMinRightDigits(int inMinRightDigits)
Updates MinRightDigits property.

Parameters:
inMinRightDigits - new value
See Also:
mnMinRightDigits

getRightPadDigit

public char getRightPadDigit()
Retrieves RightPadDigit property.

Returns:
current value
See Also:
mcRightPadDigit

setRightPadDigit

public void setRightPadDigit(char icRightPadDigit)
Updates RightPadDigit property.

Parameters:
icRightPadDigit - new value
See Also:
mcRightPadDigit

getMaxRightDigits

public int getMaxRightDigits()
Retrieves MaxRightDigits property.

Returns:
current value
See Also:
mnMaxRightDigits

setMaxRightDigits

public void setMaxRightDigits(int inMaxRightDigits)
Updates MaxRightDigits property.

Parameters:
inMaxRightDigits - new value
See Also:
mnMaxRightDigits

format

public final java.lang.String format(com.sharkysoft.math.MantissaExponent ipValue)
Formats real value.

Details: format renders the given real value (ipValue) into the format specified by the current property configuration. The value is rendered into scientific notation.

Parameters:
ipValue - the real value
Returns:
the formatted value

format

public final java.lang.String format(java.math.BigDecimal ipValue)
Formats real value.

Details: format renders the given real value (ipValue) into the format specified by the current property configuration.

Parameters:
ipValue - the real value
Returns:
the formatted value

format

public final java.lang.String format(float ifValue)
Formats real value.

Details: format renders the given real value (ifValue) into the format specified by the current property configuration.

Parameters:
ifValue - the real value
Returns:
the formatted value

format

public final java.lang.String format(double idValue)
Formats real value.

Details: format renders the given real value (idValue) into the format specified by the current property configuration.

Parameters:
idValue - the real value
Returns:
the formatted value

sharkysoft.com

Copyright © 1997-2004 Sharkysoft (sharkysoft.com). All rights reserved.