Class BigCurrencyDecimal

java.lang.Object
   |
   +----BigCurrencyDecimal

public class BigCurrencyDecimal
extends Object
A class of useful methods for financial applications that use BigIntegers internally but also have to cope with floating point numbers and strings. The idea is that 100 per cent of the rounding, formatting and parsing for an entire bookkeeping or similar application should occur in one class like this one. The code is internationalized to accept and produce decimal and currency strings based on the default or specified locale of whatever application instantiates this class.
The BDFormat class must be in the same directory as this class. BDFormat does BigDecimal formatting and parsing. There is a limit of 19 digits for the integer portion of your application's numbers. You'll get a message (in English) returned if you try to use too big of a BigDecimal.
The two-parameter morphs of displayAsDecimal() and displayAsCurrency() allow right-justified output. Just specify the field size. When needed, rounding uses the method favored by bankers in which discarded fractions > .5 round the last remaining digit away from zero, and discarded fractions == .5 round the last remaining digit up or down towards the nearest *even* digit.
Right-justified output is improved so the field size you specify is the exact length of the result string (if the result fits in the field size). With multiple instances of BigCurrencyDecimal you can display (and align decimals) accross multiple currencies at the same time. Suggestions and criticism are welcome, but use at your own risk.
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 rev. 98/12/20
Author:
Tony Dahlman

Variable Index

 o bdnf
The BDFormat class does BigDecimal formatting and parsing
 o DEFAULT_RMODE
The default rounding mode used by BigDecimal conversions.
 o digits
The number of decimal digits required by this locale's currency.
 o rmode
The rounding mode actually in effect.

Constructor Index

 o BigCurrencyDecimal()
The default constructor sets up formatting objects for the default locale.
 o BigCurrencyDecimal(Locale)
The locale-specific constructor sets up formatting objects for the requested locale.

Method Index

 o currencyStringToDecimal(String)
Numeric user input, in currency format, is converted to a double, using banker's rounding (described above) if required to achieve the locale-specified number of decimal places.
 o decimalStringToDecimal(String)
Numeric user input, in decimal format, is converted to a double, using banker's rounding (described above) if required to achieve the locale-specified number of decimal places.
 o displayAsCurrency(BigInteger)
Makes a string, formatted as currency, for output or display, as in a report, journal, etc.
 o displayAsCurrency(BigInteger, int)
Makes a right-justified string, formatted as currency, for output or display as in a report, journal, etc.
 o displayAsDecimal(BigInteger)
Makes a string, formatted as a currency-like decimal for output or display, as in a report, journal, ledger, etc.
 o displayAsDecimal(BigInteger, int)
Makes a right-justified string, formatted as a currency-like decimal for output or display, as in a report, journal, ledger, etc.
 o getRoundingMode()
The BigDecimal rounding mode (as an int) currently in effect.
 o importExternalBigDecimal(BigDecimal)
When the user or a spreadsheet supplies your application with floating point input, this method converts BigDecimal objects to internal BigInteger objects.
 o importExternalDouble(double)
When the user or a spreadsheet supplies your application with floating point input, this method converts floats and doubles to internal BigInteger objects.
 o main(String[])
Command-line test code: you can specify two arguments, the two-letter codes for language (lower case) and COUNTRY (upper case) in order to exercise this class's international features.
 o roundInternalBigDecimal(BigDecimal)
Although the calling application will use only BigIntegers internally, calculating averages or doing currency conversions will occasionally yield floating point numbers.
 o setRoundingMode(int)
Change to a different BigDecimal rounding/truncating mode

Variables

 o digits
 private int digits
The number of decimal digits required by this locale's currency.

 o DEFAULT_RMODE
 private static final int DEFAULT_RMODE
The default rounding mode used by BigDecimal conversions.

 o rmode
 private int rmode
The rounding mode actually in effect. Set it with setRoundingMode(int).

 o bdnf
 private BDFormat bdnf
The BDFormat class does BigDecimal formatting and parsing

Constructors

 o BigCurrencyDecimal
 public BigCurrencyDecimal()
The default constructor sets up formatting objects for the default locale. Locale-specific constants and limits are set. The FieldPosition object can find the decimal point in the decimal strings produced by NumberFormat.format()

 o BigCurrencyDecimal
 public BigCurrencyDecimal(Locale loc)
The locale-specific constructor sets up formatting objects for the requested locale. Use java.util.Locale class to create a Locale object. A list of 2-letter language codes is available here, and a list of 2-letter country codes is available here. Locale-specific constants are set by this constructor.

Methods

 o roundInternalBigDecimal
 BigInteger roundInternalBigDecimal(BigDecimal bd)
Although the calling application will use only BigIntegers internally, calculating averages or doing currency conversions will occasionally yield floating point numbers. This method gets rid of them by converting back to an internal BigInteger object. This implements bankers rounding (as described above).

 o importExternalDouble
 BigInteger importExternalDouble(double dval)
When the user or a spreadsheet supplies your application with floating point input, this method converts floats and doubles to internal BigInteger objects.

 o importExternalBigDecimal
 BigInteger importExternalBigDecimal(BigDecimal bd)
When the user or a spreadsheet supplies your application with floating point input, this method converts BigDecimal objects to internal BigInteger objects.

 o displayAsCurrency
 String displayAsCurrency(BigInteger bi)
Makes a string, formatted as currency, for output or display, as in a report, journal, etc. For example, you have the BigInteger value 123456. displayAsCurency() yields the strings, $1,234.56 (en_US locale) or kr 1.234,56 (da_DK locale). In Italy where they've done away with "pennies", 123456 is dislpayed as L. 123.456 .

 o displayAsCurrency
 String displayAsCurrency(BigInteger bi,
                          int fieldsize)
Makes a right-justified string, formatted as currency, for output or display as in a report, journal, etc. In this morph of the displayAsCurrency() function, you supply a BigInteger and a field size as parameters. The output will be right-justified (if possible) with decimal points aligned. We assume a monospaced font. In JDK 1.2 java.awt.font.TextLayout will open up decent, optimized, i18n methods for using prettier fonts.

Returns:
s a currency-formatted string of length fieldsize.
 o displayAsDecimal
 String displayAsDecimal(BigInteger bi)
Makes a string, formatted as a currency-like decimal for output or display, as in a report, journal, ledger, etc. For example you have the BigInteger -123456. displayAsDecimal() yields the strings, -1,234.56 (en_US locale) or -1.234,56 (da_DK locale). In Italy where they've done away with "pennies", -123456 is displayed as -123.456 .

 o displayAsDecimal
 String displayAsDecimal(BigInteger bi,
                         int fieldsize)
Makes a right-justified string, formatted as a currency-like decimal for output or display, as in a report, journal, ledger, etc. In this morph of the displayAsDecimal() function, you provide a a BigInteger and a field size as parameters. The output will be right-justified (if possible) with decimals aligned. We assume a monospaced font. In JDK 1.2 java.awt.font.TextLayout will open up decent, optimized, i18n methods for using prettier fonts.

Returns:
s a currency-like decimal string of length fieldsize.
 o decimalStringToDecimal
 BigDecimal decimalStringToDecimal(String input)
Numeric user input, in decimal format, is converted to a double, using banker's rounding (described above) if required to achieve the locale-specified number of decimal places. Returns BigDecimal 0.0 if parsing fails.

 o currencyStringToDecimal
 BigDecimal currencyStringToDecimal(String input)
Numeric user input, in currency format, is converted to a double, using banker's rounding (described above) if required to achieve the locale-specified number of decimal places. Returns BigDecimal 0.0 if parsing fails.

 o getRoundingMode
 int getRoundingMode()
The BigDecimal rounding mode (as an int) currently in effect.

 o setRoundingMode
 void setRoundingMode(int i)
Change to a different BigDecimal rounding/truncating mode

 o main
 public static void main(String args[]) throws IOException, NumberFormatException
Command-line test code: you can specify two arguments, the two-letter codes for language (lower case) and COUNTRY (upper case) in order to exercise this class's international features.
If you specify foreign locales, be sure to use its correct decimal separator and sign convention (e.g. parentheses for negative currency values in the en_US locale) when supplying input. A list of 2-letter language codes is available here, and a list of 2-letter country codes is available here. Even if your operating system implements unicode, all the fonts for all the countries and languages may not be available.
The prompts and text print-out here are all in English, although the numbers, currencies, sign conventions, etc. should fully implement "i18n".