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
-
bdnf
- The BDFormat class does BigDecimal formatting and parsing
-
DEFAULT_RMODE
- The default rounding mode used by BigDecimal conversions.
-
digits
- The number of decimal digits required by this locale's currency.
-
rmode
- The rounding mode actually in effect.
-
BigCurrencyDecimal()
- The default constructor sets up formatting objects for the default
locale.
-
BigCurrencyDecimal(Locale)
- The locale-specific constructor sets up formatting objects for the
requested locale.
-
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.
-
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.
-
displayAsCurrency(BigInteger)
- Makes a string, formatted as currency, for output or
display, as in a report, journal, etc.
-
displayAsCurrency(BigInteger, int)
- Makes a right-justified string, formatted as currency, for output
or display as in a report, journal, etc.
-
displayAsDecimal(BigInteger)
- Makes a string, formatted as a currency-like decimal for
output or display, as in a report, journal, ledger, etc.
-
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.
-
getRoundingMode()
- The BigDecimal rounding mode (as an int) currently in effect.
-
importExternalBigDecimal(BigDecimal)
- When the user or a spreadsheet supplies your application
with floating point input, this method converts BigDecimal objects
to internal BigInteger objects.
-
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.
-
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.
-
roundInternalBigDecimal(BigDecimal)
- Although the calling application will use only BigIntegers internally,
calculating averages or doing currency conversions will occasionally yield
floating point numbers.
-
setRoundingMode(int)
- Change to a different BigDecimal rounding/truncating mode
digits
private int digits
- The number of decimal digits required by this locale's currency.
DEFAULT_RMODE
private static final int DEFAULT_RMODE
- The default rounding mode used by BigDecimal conversions.
rmode
private int rmode
- The rounding mode actually in effect. Set it with setRoundingMode(int).
bdnf
private BDFormat bdnf
- The BDFormat class does BigDecimal formatting and parsing
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()
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.
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).
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.
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.
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 .
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.
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 .
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.
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.
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.
getRoundingMode
int getRoundingMode()
- The BigDecimal rounding mode (as an int) currently in effect.
setRoundingMode
void setRoundingMode(int i)
- Change to a different BigDecimal rounding/truncating mode
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".