Class BDFormat

java.lang.Object
   |
   +----BDFormat

public class BDFormat
extends Object

Convert between Strings, in locale-specific currency or decimal format, and BigDecimal objects with this class's five methods. The java.text classes, NumberFormat and DecimalFormat, seem to implement partial formatting from and parsing into BigDecimal objects. Here is code to cope with this as of JDK 1.1.6. Multiple instances across locales is supported so you can display various currencies with decimal point aligned on the same page. Decimal-point-aligned output is supported, meaning let me know if it doesn't work and I'll see what I can do.... ;) Use at your own risk.

A limitation of no more than 19 integer digits is due to the underlying java code. Thanks to Dean Riechman for pointing this out. New constructors (Nov 1998) allow you to set the rounding mode and number of decimal places when formatting non-currency decimals.

Here's a little test routine that shows off what BDFormat can do:


import java.util.*;     // for locales
import java.io.*;       // for I/O to console      
public class TestBDFormat {
   public static void main(String[] args) {
      PrintWriter outs = new PrintWriter( new BufferedWriter( 
                  new OutputStreamWriter( System.out ) ), true );
      String dots = "........................................"; // 40 dots
      String input;
      if (args.length < 1) 
         input = "1234567890123456789.01500";
      else 
         input = args[0];
      com.ibm.math.BigDecimal bd = new BDFormat().parse(input);
      Locale[] locs = java.text.NumberFormat.getAvailableLocales();
      StringBuffer output = new StringBuffer(80);
      for (int i=0; i < locs.length; i++) {
         Locale loc = locs[i];
         output.setLength(0);
         output.append( loc.getLanguage() );        output.append( '_' );
         output.append( loc.getCountry() );         output.append( "  " );
         output.append( loc.getDisplayLanguage() ); output.append(" - ");
         output.append( loc.getDisplayCountry() );
         int len = output.length();
         // 39 spaces text + 40 spaces for number field
         output.append(dots.substring( 40 - (39 - len)));
         output.append(new BDFormat(loc).curformat(bd,40));
         outs.println( output.toString() );
      }
      outs.close();
   }
} 

This version uses IBM's new BigDecimal class. The com.ibm.math package must be available. If you have "decimal.jar" from IBM alphaWorks, put an entry in your classpath pointing to it, e.g. SET CLASSPATH=%classpath%;f:\Java11\lib\decimal.jar. With Java release level 1.1 you'll also need to add the java/lang/Comparable interface (just one abstract method declaration) to your classes.zip.

Version:
1.2 1998/12/20
Author:
Tony Dahlman

Variable Index

 o bd
 o cf
 o curDigits
Number of fractional digits in this locale's currency.
 o decDigits
Number of decimal digits for use in non-currency format() methods.
 o decFractionSet
Flag whether number of decimal digits is being preset.
 o decRMode
The rounding mode for non-currency formatting
 o dots
Eighty dots for use by the decimal-point aligning formatters.
 o dsep
Special characters needed mostly for parsing.
 o ifp
Marks the integer portion of formatted strings.
 o minus
Special characters needed mostly for parsing.
 o negpre
Characters before and after negatives, which may be needed for parsing.
 o negsuf
Characters before and after negatives, which may be needed for parsing.
 o nf
 o rmode
Rounding mode preset to banker-favored method for use by parse() and curformat() methods.
 o spaces
Ten spaces for padding after formatting for currency

Constructor Index

 o BDFormat()
Uses the default locale to set some instance variables like decimal separator, negative prefix, etc..
 o BDFormat(int, int)
Uses the default locale to set some instance variables, and causes the format() methods to display a set number of fractional digits, using the specified rounding mode if necessary.
 o BDFormat(Locale)
Uses the specified locale to set some instance variables like decimal separator, negative prefix, etc.
 o BDFormat(Locale, int, int)
Uses the specified locale to set some instance variables, and causes the format() methods to display a set number of fractional digits, using the specified rounding mode if necessary.

Method Index

 o curformat(BigDecimal)
A currency format method: feed it a BigDecimal and get back a String with currency symbol, commas, dots or spaces (as appropriate to the locale), and the locale-specific decimal separator.
 o curformat(BigDecimal, int)
A right-justified currency format method: feed it a BigDecimal and get back a String with currency symbol, commas, dots or spaces (as appropriate to the locale), and the locale-specific decimal separator.
 o format(BigDecimal)
A format method: feed it a BigDecimal and get back a String with commas, dots or spaces (as appropriate to the locale), and the locale-specific decimal separator.
 o format(BigDecimal, int)
A right-justified format method: feed it a BigDecimal and get back a String with commas, dots or spaces (as appropriate to the locale), and the locale-specific decimal separator.
 o main(String[])
Command-line test code to exercise the class's methods.
 o parse(String)
A parse method: feed it a String and maybe get it to give you a full-blown BigDecimal object in return.

Variables

 o bd
 private BigDecimal bd
 o nf
 private NumberFormat nf
 o cf
 private NumberFormat cf
 o ifp
 private FieldPosition ifp
Marks the integer portion of formatted strings. Used for aligning decimal points in the two-parameter versions of format() and curformat().

 o dsep
 private char dsep
Special characters needed mostly for parsing.

 o minus
 private char minus
Special characters needed mostly for parsing.

 o negpre
 private String negpre
Characters before and after negatives, which may be needed for parsing.

 o negsuf
 private String negsuf
Characters before and after negatives, which may be needed for parsing.

 o decFractionSet
 private boolean decFractionSet
Flag whether number of decimal digits is being preset. This flag is set when one of the constructors is used which takes "int digits" and "int rmode" as parameters.

 o decDigits
 private int decDigits
Number of decimal digits for use in non-currency format() methods. Default number of decimal digits is 6 but has no effect unless decFractionSet is true. To set a particular number of digits for non-currency formatting and parsing, use one of the constructors that takes "int digits" and "int rmode" as parameters.

 o decRMode
 private int decRMode
The rounding mode for non-currency formatting

 o curDigits
 private int curDigits
Number of fractional digits in this locale's currency.

 o rmode
 private int rmode
Rounding mode preset to banker-favored method for use by parse() and curformat() methods.

 o dots
 protected static final String dots
Eighty dots for use by the decimal-point aligning formatters.

 o spaces
 protected static final String spaces
Ten spaces for padding after formatting for currency

Constructors

 o BDFormat
 public BDFormat()
Uses the default locale to set some instance variables like decimal separator, negative prefix, etc..

 o BDFormat
 public BDFormat(Locale loc)
Uses the specified locale to set some instance variables like decimal separator, negative prefix, etc. If NumberFormat.getXXXInstance() has given us something other than an instance of DecimalFormat, we throw an exception.

Parameters:
loc - - an instance of java.util.Locale.
 o BDFormat
 public BDFormat(Locale loc,
                 int digits,
                 int rmode)
Uses the specified locale to set some instance variables, and causes the format() methods to display a set number of fractional digits, using the specified rounding mode if necessary.

Parameters:
loc - - an instance of java.util.Locale.
digits - - for non-currency decimals, the number of decimal places.
rmode - - one of the BigDecimal rounding modes in case you are displaying fewer decimal places than are available from your input to one of the format() methods.
 o BDFormat
 public BDFormat(int digits,
                 int rmode)
Uses the default locale to set some instance variables, and causes the format() methods to display a set number of fractional digits, using the specified rounding mode if necessary.

Parameters:
digits - - for non-currency decimals, the number of decimal places.
rmode - - one of the BigDecimal rounding modes in case you are displaying fewer decimal places than are available from your input to one of the format() methods.

Methods

 o parse
 BigDecimal parse(String input)
A parse method: feed it a String and maybe get it to give you a full-blown BigDecimal object in return. Throws an exception if the parse fails. Should parse either currency strings or regular decimal strings indiscrimately.

 o format
 String format(BigDecimal bdec)
A format method: feed it a BigDecimal and get back a String with commas, dots or spaces (as appropriate to the locale), and the locale-specific decimal separator. Fails if the integer portion of the decimal number is bigger than nineteen digits (10 million trillion American nomenclature).

 o format
 String format(BigDecimal bdec,
               int fieldsize)
A right-justified format method: feed it a BigDecimal and get back a String with commas, dots or spaces (as appropriate to the locale), and the locale-specific decimal separator. Just specify the field size. When the number of decimal digits is less than 4, the decimal points will be aligned, regardless of locale, in every case I know about. If the number to be formatted is too large for the requested field size, the result String will be longer than what you asked for. Formatting fails if the integer portion of the BigDecimal is greater than 19 digits: you'll get a message instead of a number.

 o curformat
 String curformat(BigDecimal bdec)
A currency format method: feed it a BigDecimal and get back a String with currency symbol, commas, dots or spaces (as appropriate to the locale), and the locale-specific decimal separator. Fails if the integer portion of the decimal number is bigger than nineteen digits.

 o curformat
 String curformat(BigDecimal bdec,
                  int fieldsize)
A right-justified currency format method: feed it a BigDecimal and get back a String with currency symbol, commas, dots or spaces (as appropriate to the locale), and the locale-specific decimal separator. Just specify the field size. When the number of decimal digits is less than 4, the decimal points will be aligned, regardless of locale, in every case I know about. If the number to be formatted is too large for the requested field size, the result String will be longer than what you asked for. Formatting fails if the integer portion of the BigDecimal is greater than 19 digits: you'll get a message instead of a number.

 o main
 public static void main(String args[]) throws IOException
Command-line test code to exercise the class's methods. Specify the two-letter codes for language (lower case) and COUNTRY (upper case) on the command-line to try out the international capability. Be sure the numbers entered use the correct decimal and grouping separators for the selected locale.