Class PaddedDecimalFormat

java.lang.Object
   |
   +----java.text.Format
           |
           +----java.text.NumberFormat
                   |
                   +----java.text.DecimalFormat
                           |
                           +----PaddedDecimalFormat

public class PaddedDecimalFormat
extends DecimalFormat

This class adds easy right-justified output (formatting) and alignment of the decimal separator to the existing capabilities of java.text.DecimalFormat and its parent class, java.text.NumberFormat.

Create a formatter/parser using one of the factory methods of the form, getXXXInstance( int decPlaces, Locale loc). Specify the maximum number of decimal places your program will need, across all locales where it will be run. Make (instantiate) a separate formatter object for each format type (Number, Currency, or Percent). Use Locale.getDefault() to localize the formatting and parsing to whatever a user's particular language and country may be. Or you can specify the Locale variables and then display output for several locales at once.

If the decPlaces variable is the same and you do not change the default alignField variable, the decimal separators will align across all the different formatters. Use the same fieldsize in your calls to the different objects' format() methods.

The following additions are provided to what is already available from the core java.text.DecimalFormat class:


For compatibility with code you may have written already, all three standard DecimalFormat constructors are provided. But to really take advantage of this class's raison d'être, use one of the getXXXInstance factory constructors wherever you can. The standard constructors allow you to use PaddedDecimalFormat wherever you have used DecimalFormat in your existing code.

Use at your own risk! If you find bugs, please let me know.

Version:
1.0 99/08/19 - Original version
Author:
Tony Dahlman

Variable Index

 o alignField
 o decPlaces
 o defaultCurPlaces
 o fp
 o loc
 o maxFieldSize
 o padCharacter
 o spaces

Constructor Index

 o PaddedDecimalFormat()
Default constructor.
 o PaddedDecimalFormat(String)
Mimics the usual DecimalFormat constructor, allowing you to use this class everywhere your existing code uses DecimalFormat.
 o PaddedDecimalFormat(String, DecimalFormatSymbols)
The full-function DecimalFormat constructor.

Method Index

 o format(double, int)
Right-justified double-to-String conversion.
 o getAlignField()
Get the current setting for alignField.
 o getCurrencyInstance(int, Locale)
Static method returns an instance, set up for a particular locale and number of decimal places.
 o getDecPlaces()
Get the current setting for the number of decimal places specified when this formatter was constructed.
 o getFractionDigits()
Get current setting for both maximum and minimum number of fraction digits.
 o getLoc()
Get the current locale setting.
 o getLocalizedPattern()
Use DecimalFormat's toLocalizedPattern() method to get this formatter's current formatting string.
 o getMaxFieldSize()
Get current setting for maximum fieldsize.
 o getNumberInstance(int, Locale)
Static method returns an instance, set up for a particular locale and number of decimal places.
 o getPadCharacter()
Get the pad character.
 o getPattern()
Use DecimalFormat's toPattern() method to get this formatter's current formatting string.
 o getPercentInstance(int, Locale)
Static method returns an instance, set up for a particular locale and number of decimal places.
 o setAlignField(int)
Control whether alignment will occur at the decimal separator or at the last numeric digit.
 o setFractionDigits(int)
Set both the maximum and minimum number of fraction digits to be displayed.
 o setLocalizedPattern(String)
Attempt to change this formatting object by supplying a new DecimalFormat pattern string.
 o setMaxFieldSize(int)
Set the maximum fieldsize: useful if you need more than the default value of 80.
 o setPadCharacter(char)
Set the pad character.
 o setPattern(String)
Attempt to change this formatting object by supplying a new DecimalFormat pattern string.

Variables

 o decPlaces
 protected int decPlaces
 o loc
 protected Locale loc
 o padCharacter
 protected char padCharacter
 o maxFieldSize
 protected int maxFieldSize
 o defaultCurPlaces
 protected int defaultCurPlaces
 o alignField
 protected int alignField
 o fp
 protected FieldPosition fp
 o spaces
 protected String spaces

Constructors

 o PaddedDecimalFormat
 public PaddedDecimalFormat()
Default constructor. Useful for JavaBeans (good luck!) but not much else.

See Also:
getNumberInstance, getPercentInstance, getCurrencyInstance
 o PaddedDecimalFormat
 public PaddedDecimalFormat(String pattern)
Mimics the usual DecimalFormat constructor, allowing you to use this class everywhere your existing code uses DecimalFormat.

See Also:
getNumberInstance, getPercentInstance, getCurrencyInstance
 o PaddedDecimalFormat
 public PaddedDecimalFormat(String pattern,
                            DecimalFormatSymbols dfs)
The full-function DecimalFormat constructor. Included here for use by the getXXXInstance() methods which follow. It is public simply to provide complete compatibility with existing DecimalFormat code.

See Also:
getNumberInstance, getPercentInstance, getCurrencyInstance

Methods

 o getNumberInstance
 public static PaddedDecimalFormat getNumberInstance(int decPlaces,
                                                     Locale loc)
Static method returns an instance, set up for a particular locale and number of decimal places. This is the easy way to get right-justified, decimal-aligned output.

Parameters:
decPlaces - Number of decimal places to plan for. Make this the maximum number of decimal places your application will use, and keep its value constant across all the formatters you may instantiate.
loc - The Locale (language and country combination).
 o getCurrencyInstance
 public static PaddedDecimalFormat getCurrencyInstance(int decPlaces,
                                                       Locale loc)
Static method returns an instance, set up for a particular locale and number of decimal places. This is the easy way to get right-justified, decimal-aligned output.

Parameters:
decPlaces - Number of decimal places to plan for. Make this the maximum number of decimal places your application will use, and keep its value constant across all the formatters you may instantiate.
loc - The Locale (language and country combination).
 o getPercentInstance
 public static PaddedDecimalFormat getPercentInstance(int decPlaces,
                                                      Locale loc)
Static method returns an instance, set up for a particular locale and number of decimal places. This is the easy way to get right-justified, decimal-aligned output.

Parameters:
decPlaces - Number of decimal places to plan for. Make this the maximum number of decimal places your application will use, and keep its value constant across all the formatters you may instantiate.
loc - The Locale (language and country combination).
 o format
 String format(double dval,
               int fieldsize)
Right-justified double-to-String conversion. Default behavior is to return decimal aligned decimal strings, as long as the fieldsize is large enough.

Parameters:
dval - a double (small "d") value. Returns an error message for values larger than the maximum (smaller than the minimum) Long integer.
fieldsize - desired number of monospaced characters, including padding. The fieldsize must be less than the maxFieldSize, which defaults to 80.
See Also:
setMaxFieldSize, setAlignField, setPadCharacter
 o setPadCharacter
 public void setPadCharacter(char ch)
Set the pad character.

 o getPadCharacter
 public char getPadCharacter()
Get the pad character.

 o setMaxFieldSize
 public void setMaxFieldSize(int size)
Set the maximum fieldsize: useful if you need more than the default value of 80.

 o getMaxFieldSize
 public int getMaxFieldSize()
Get current setting for maximum fieldsize.

 o setFractionDigits
 public void setFractionDigits(int places)
Set both the maximum and minimum number of fraction digits to be displayed.

Parameters:
places - Values can be 0 to the decPlaces value used to create this formatting object. Specify -1 to indicate you want the default currency format's maximum number of decimal places.
 o getFractionDigits
 public int getFractionDigits()
Get current setting for both maximum and minimum number of fraction digits.

Returns:
a return value of -1 indicates setFractionDigits() has not yet been called.
 o setAlignField
 public void setAlignField(int alignField)
Control whether alignment will occur at the decimal separator or at the last numeric digit.

Parameters:
alignField - Can be NumberFormat.INTEGER_FIELD (the default) which causes alignment of decimal separators, or NumberFormat.FRACTION_FIELD which causes alignment of the last numeric digit.
 o getAlignField
 public int getAlignField()
Get the current setting for alignField. Compare the int result with the NumberFormat constants, INTEGER_FIELD and FRACTION_FIELD.

 o setPattern
 public void setPattern(String pat)
Attempt to change this formatting object by supplying a new DecimalFormat pattern string. Throws IllegalArgumentException if the attempt fails. For example, switching the decimal separator to be a grouping separator will fail. Use a new formatting object rather than setPattern() to switch among locales.
This method uses DecimalFormat's applyPattern() method. I have not found the applyLocalizedPattern() method useful.

 o getPattern
 public String getPattern()
Use DecimalFormat's toPattern() method to get this formatter's current formatting string. I have not found the toLocalizedPattern() method useful.

 o setLocalizedPattern
 public void setLocalizedPattern(String pat)
Attempt to change this formatting object by supplying a new DecimalFormat pattern string. Throws IllegalArgumentException if the attempt fails.
Uses DecimalFormat's applyLocalizedPattern() method and provided here for "completeness." I haven't found it useful.

 o getLocalizedPattern
 public String getLocalizedPattern()
Use DecimalFormat's toLocalizedPattern() method to get this formatter's current formatting string. Provided only for "completeness": I haven't found it useful.

 o getDecPlaces
 public int getDecPlaces()
Get the current setting for the number of decimal places specified when this formatter was constructed. This value should not be changed for the life of this formatting object. All formatting objects should use the same value to permit easy decimal alignment.

 o getLoc
 public Locale getLoc()
Get the current locale setting. Use separate formatting objects for each needed locale (language/country combination). To cause formatting to localize for any particular user's locale, call the constructor with Locale.getDefault() as the Locale variable.