Sharkysoft home

lava.text
Class RealFormatter

java.lang.Object
  |
  +--lava.text.StringJustifier
        |
        +--lava.text.NumberFormatter
              |
              +--lava.text.RealFormatter

public class RealFormatter
extends NumberFormatter

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

exponent_formatter

public final IntegerFormatter exponent_formatter
IntegerFormatter used to format exponents when scientific notation is used. Consult lava.math.IntegerFormatter for more details. Default settings: min_digits=2, zero_prefix="E", pos_prefix="E+", neg_prefix="E-".

max_left_digits

public int max_left_digits
The maximum number of digits to show left of the decimal point. The number will "wrap" if it is too large. Default value: Integer.MAX_VALUE.

min_left_digits

public int min_left_digits
The minimum number of digits to the left of the decimal point. If the number isn't large enough, the "extra places" will be set to left_pad_digit. Default value: 1.

left_pad_digit

public char left_pad_digit
Digit or character to use for left-padding numbers. Used only when the value of 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'.

show_dec_point

public boolean show_dec_point
Determines whether or not the decimal point will be output when it is possible to omit it. Default value: false .

min_right_digits

public int min_right_digits
The minimum number of digits to show to the right of the decimal point. If the fractional portion of the number isn't long enough, the extra places will be set to right_pad_digit. Default value: 0.

right_pad_digit

public char right_pad_digit
Digit or character to use for right-padding numbers. Used only when the value of 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'.

max_right_digits

public int max_right_digits
The maximum number of digits to show to 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 the value set for 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

RealFormatter

public RealFormatter()
Initializes a new RealFormatter with default settings.
Method Detail

format

public final java.lang.String format(java.math.BigDecimal value)
Formats a real number.
Parameters:
value - the real number
Returns:
a formatted representation of the real number

format

public final java.lang.String format(ScientificNotationBigDecimal value)
Formats a real number in scientific notation.
Parameters:
value - the real number
Returns:
the real number formatted in scientific notation

format

public final java.lang.String format(float value)
Formats a real number.
Parameters:
value - the real number
Returns:
a formatted representation of the real number

format

public final java.lang.String format(double value)
Formats a real number.
Parameters:
value - the real number
Returns:
a formatted representation of the real number

Sharkysoft home