|
sharkysoft.com | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.sharkysoft.printf.engine.StringFormatter com.sharkysoft.printf.engine.NumberFormatter com.sharkysoft.printf.engine.RealFormatter
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-
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-
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 |
protected final IntegerFormatter mpExponentFormatter
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-".
getExponentFormatter()
protected int mnMaxLeftDigits
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
.
getMaxLeftDigits()
,
setMaxLeftDigits(int)
protected int mnMinLeftDigits
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.
getMinLeftDigits()
,
setMinLeftDigits(int)
protected char mcLeftPadDigit
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'.
getLeftPadDigit()
,
setLeftPadDigit(char)
protected boolean mzShowDecPoint
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
.
getShowDecPoint()
,
setShowDecPoint(boolean)
protected int mnMinRightDigits
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.
getMinRightDigits()
,
setMinRightDigits(int)
protected char mcRightPadDigit
Details: Property RightPadDigit
determines the digit
or character to use for right-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'.
getRightPadDigit()
,
setRightPadDigit(char)
protected int mnMaxRightDigits
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
.
getMaxRightDigits()
,
setMaxRightDigits(int)
Constructor Detail |
public RealFormatter()
Details: This constructor initializes a new instance with the default value for each property, as specified in the property documentation.
Method Detail |
public IntegerFormatter getExponentFormatter()
mpExponentFormatter
public int getMaxLeftDigits()
mnMaxLeftDigits
public void setMaxLeftDigits(int inMaxLeftDigits)
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.
inMaxLeftDigits
- new valuemnMaxLeftDigits
public int getMinLeftDigits()
mnMinLeftDigits
public void setMinLeftDigits(int inMinLeftDigits)
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.
inMinLeftDigits
- new valuemnMinLeftDigits
public char getLeftPadDigit()
mcLeftPadDigit
public void setLeftPadDigit(char icLeftPadDigit)
icLeftPadDigit
- new valuemcLeftPadDigit
public boolean getShowDecPoint()
mzShowDecPoint
public void setShowDecPoint(boolean izShowDecPoint)
izShowDecPoint
- mzShowDecPoint
public int getMinRightDigits()
mnMinRightDigits
public void setMinRightDigits(int inMinRightDigits)
inMinRightDigits
- new valuemnMinRightDigits
public char getRightPadDigit()
mcRightPadDigit
public void setRightPadDigit(char icRightPadDigit)
icRightPadDigit
- new valuemcRightPadDigit
public int getMaxRightDigits()
mnMaxRightDigits
public void setMaxRightDigits(int inMaxRightDigits)
inMaxRightDigits
- new valuemnMaxRightDigits
public final java.lang.String format(com.sharkysoft.math.MantissaExponent ipValue)
Details: format
renders the given real value
(ipValue
) into the format specified by the current property
configuration. The value is rendered into scientific notation.
ipValue
- the real value
public final java.lang.String format(java.math.BigDecimal ipValue)
Details: format
renders the given real value
(ipValue
) into the format specified by the current property
configuration.
ipValue
- the real value
public final java.lang.String format(float ifValue)
Details: format
renders the given real value
(ifValue
) into the format specified by the current property
configuration.
ifValue
- the real value
public final java.lang.String format(double idValue)
Details: format
renders the given real value
(idValue
) into the format specified by the current property
configuration.
idValue
- the real value
|
sharkysoft.com | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Copyright © 1997-2004 Sharkysoft (sharkysoft.com). All rights reserved.