Class NumberConverter
java.lang.Object
|
+----NumberConverter
- public class NumberConverter
- extends Object
This class is just a trivial demo of how to do conversions between
strings and numbers in a fully WORA (Write Once Run Anywhere) way.
Java's BigDecimal class and my own BDFormat class are used (instead
of Java's Double, Long and NumberFormat classes) for two main
reasons.
1. Double and Long store numbers as binaries, creating subtle
pitfalls and limits with which a programmer using those classes
must become familiar if unintended results are to be avoided.
While Double and Long perform better (they take advantage of the
CPU's floating point routines), speed in financial and arithmetic
operations is rarely as important as accuracy.
2. NumberFormat has two major limitations in its current form: its
format() routine does not handle BigDecimal objects correctly, and
its parse() routine does not treat spaces correctly in locales that
use the space as a grouping separator. My BDFormat class makes
extensive use of NumberFormat's format() and parse() methods but
adds the functionality to fix both these problems. Also, an attempt
to format() a number too large for NumberFormat will now throw an
exception instead of just quietly returning incorrect output.
Finally, a greatly imporved BigDecimal class is now available as
com.ibm.math.BigDecimal. It's up to 23-times faster and it does
decimal arithmetic in a way humans (other than numerics specialists)
would expect. Get it from
IBM alphaWorks.
To use this class (while you get used to Java's "new math" as I see it),
just instantiate (assign a variable to) NumberConverter and call one of
the methods, as in:
BigDecimal big = new BigDecimal( someNumber );
String str = new NumberConverter().writeDecimal( big );
or
Locale loc_fr = new Locale( "fr", "FR" );
String input = "-12 345,678 Fr";
BigDecimal bd_in = new NumberConverter(loc).readInteger( input );
// bd_in now contains a BigDecimal whose value is -12346 (after rounding)
To use the BDFormat class directly (once you are comfortable with this
"new math"), instantiate BDFormat and call one of the methods. For example,
to display an amount in several currencies, with decimals aligned in a 30-column field,
you can write:
BigDecimal dollarAmount = new BigDecimal( amountAsDoubleOrString );
BigDecimal francsPerDollar = new BigDecimal(5.6755);
BigDecimal liraPerDollar = new BigDecimal(1676.00);
BigDecimal kronerPerDollar = new BigDecimal(6.4310);
// if using System.out, and en_US is the default locale, you can write:
System.out.println(
"Soccer tickets........." + new BDFormat().curformat( dollarAmount, 30) + "\n"
+"Billets du football...." + new BDFormat( new Locale("fr","FR") ).curformat(
dollarAmount.multiply(francsPerDollar), 30) + "\n"
+"Biglietti di soccer...." + new BDFormat( new Locale("it","IT") ).curformat(
dollarAmount.multiply(liraPerDollar), 30) + "\n"
+"Fodboldbilletter......." + new BDFormat( new Locale("da","DK") ).curformat(
dollarAmount.multiply(kronerPerDollar), 30) );
which gives the output:
Soccer tickets.......................... $195.28
Billets du football.................... 1 108,31 F
Biglietti di soccer............... L. 327.289
Fodboldbilletter.................... kr 1.255,85
- Version:
- 1.0 - November 1998
- Author:
- Tooy Dahlman
-
nf
- The BigDecimal number formatter.
-
roundingMode
- The usual rounding mode for financial apps, known as "HALF_EVEN".
-
NumberConverter()
- Default constructor, uses the default locale.
-
NumberConverter(Locale)
- Locale-specific constructor.
-
main(String[])
- Test routine: try out the parsing and formatting methods.
-
readDecimal(String)
- Get a BigDecimal from the string you supply.
-
readInteger(String)
- Get a BigDecimal (less any decimal fraction) from the string you supply.
-
writeDecimal(BigDecimal)
- Get a formatted string from the BigDecimal amount you supply.
-
writeInteger(BigDecimal)
- Get a formatted string from the BigDecimal amount you supply.
nf
private BDFormat nf
- The BigDecimal number formatter.
roundingMode
private int roundingMode
- The usual rounding mode for financial apps, known as "HALF_EVEN".
NumberConverter
public NumberConverter()
- Default constructor, uses the default locale.
NumberConverter
public NumberConverter(Locale loc)
- Locale-specific constructor.
readInteger
public BigDecimal readInteger(String input)
- Get a BigDecimal (less any decimal fraction) from the string you supply.
readDecimal
public BigDecimal readDecimal(String input)
- Get a BigDecimal from the string you supply.
writeInteger
public String writeInteger(BigDecimal amount)
- Get a formatted string from the BigDecimal amount you supply.
writeDecimal
public String writeDecimal(BigDecimal amount)
- Get a formatted string from the BigDecimal amount you supply.
main
public static void main(String args[]) throws IOException
- Test routine: try out the parsing and formatting methods.