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
-
bd
-
-
cf
-
-
curDigits
- Number of fractional digits in this locale's currency.
-
decDigits
- Number of decimal digits for use in non-currency format() methods.
-
decFractionSet
- Flag whether number of decimal digits is being preset.
-
decRMode
- The rounding mode for non-currency formatting
-
dots
- Eighty dots for use by the decimal-point aligning formatters.
-
dsep
- Special characters needed mostly for parsing.
-
ifp
- Marks the integer portion of formatted strings.
-
minus
- Special characters needed mostly for parsing.
-
negpre
- Characters before and after negatives, which may be needed
for parsing.
-
negsuf
- Characters before and after negatives, which may be needed
for parsing.
-
nf
-
-
rmode
- Rounding mode preset to banker-favored method for use by parse()
and curformat() methods.
-
spaces
- Ten spaces for padding after formatting for currency
-
BDFormat()
- Uses the default locale to set some instance variables like decimal
separator, negative prefix, etc..
-
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.
-
BDFormat(Locale)
- Uses the specified locale to set some instance variables like decimal
separator, negative prefix, etc.
-
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.
-
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.
-
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.
-
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.
-
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.
-
main(String[])
- Command-line test code to exercise the class's methods.
-
parse(String)
- A parse method: feed it a String and maybe get it to
give you a full-blown BigDecimal object in return.
bd
private BigDecimal bd
nf
private NumberFormat nf
cf
private NumberFormat cf
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().
dsep
private char dsep
- Special characters needed mostly for parsing.
minus
private char minus
- Special characters needed mostly for parsing.
negpre
private String negpre
- Characters before and after negatives, which may be needed
for parsing.
negsuf
private String negsuf
- Characters before and after negatives, which may be needed
for parsing.
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.
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.
decRMode
private int decRMode
- The rounding mode for non-currency formatting
curDigits
private int curDigits
- Number of fractional digits in this locale's currency.
rmode
private int rmode
- Rounding mode preset to banker-favored method for use by parse()
and curformat() methods.
dots
protected static final String dots
- Eighty dots for use by the decimal-point aligning formatters.
spaces
protected static final String spaces
- Ten spaces for padding after formatting for currency
BDFormat
public BDFormat()
- Uses the default locale to set some instance variables like decimal
separator, negative prefix, etc..
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.
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.
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.
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.
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).
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.
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.
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.
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.