|
Sharkysoft home | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--lava.text.StringJustifier | +--lava.text.NumberFormatter | +--lava.text.RealFormatter
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 . field_width = 25; formatter . cropping = JUSTIFY.RIGHT; formatter . min_right_digits = 2; formatter . max_right_digits = 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 | |
IntegerFormatter |
exponent_formatter
IntegerFormatter used to format exponents when scientific notation is used. |
char |
left_pad_digit
Digit or character to use for left-padding numbers. |
int |
max_left_digits
The maximum number of digits to show left of the decimal point. |
int |
max_right_digits
The maximum number of digits to show to the right of the decimal point. |
int |
min_left_digits
The minimum number of digits to the left of the decimal point. |
int |
min_right_digits
The minimum number of digits to show to the right of the decimal point. |
char |
right_pad_digit
Digit or character to use for right-padding numbers. |
boolean |
show_dec_point
Determines whether or not the decimal point will be output when it is possible to omit it. |
Fields inherited from class lava.text.NumberFormatter |
neg_prefix,
neg_suffix,
pos_prefix,
pos_suffix,
radix,
sig_digits,
uppercase,
zero_prefix,
zero_suffix |
Fields inherited from class lava.text.StringJustifier |
cropping,
field_width,
justification,
pad_char |
Constructor Summary | |
RealFormatter()
Initializes a new RealFormatter with default settings. |
Method Summary | |
java.lang.String |
format(java.math.BigDecimal value)
Formats a real number. |
java.lang.String |
format(double value)
Formats a real number. |
java.lang.String |
format(float value)
Formats a real number. |
java.lang.String |
format(ScientificNotationBigDecimal value)
Formats a real number in scientific notation. |
Methods inherited from class lava.text.StringJustifier |
format,
format,
toString |
Methods inherited from class java.lang.Object |
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
wait,
wait,
wait |
Field Detail |
public final IntegerFormatter exponent_formatter
lava.math.IntegerFormatter
for more details. Default settings: min_digits=2, zero_prefix="E", pos_prefix="E+", neg_prefix="E-".public int max_left_digits
Integer.MAX_VALUE
.public int min_left_digits
left_pad_digit
. Default value: 1.public char left_pad_digit
min_left_digits
requires it. If set to 0, the space character will be used, but the prefix string will appear between the pad digits and the actual number. Default value: '0'.public boolean show_dec_point
false
.public int min_right_digits
right_pad_digit
. Default value: 0.public char right_pad_digit
min_right_digits
requires it. If set to 0, the space character will be used, but the suffix string will appear between the pad digits and the actual number. Default value: '0'.public int max_right_digits
max_right_digits
is incompatible with the value set for sig_digits
for a particular value, the value will first be set to have the appropriate number of significant digits, and then trailing zeros will be trimmed, if there are any, until the max_right_digits constraint is satisfied or until there are no more trailing zeros. Default value: Integer . MAX_VALUE.Constructor Detail |
public RealFormatter()
Method Detail |
public final java.lang.String format(java.math.BigDecimal value)
value
- the real numberpublic final java.lang.String format(ScientificNotationBigDecimal value)
value
- the real numberpublic final java.lang.String format(float value)
value
- the real numberpublic final java.lang.String format(double value)
value
- the real number
|
Sharkysoft home | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |