Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Money Format + Decimal Place Format

Thread view: 
shannon - 01 Feb 2006 21:35 GMT
Hi,

I want to output a figure that includes two decimal places and currency
format.  However, I can't get both to work.  This is what i have been
working on so far. the decimal format below still returns a whole
number

  outputTextArea.append( "Product 1" + "\t" + twoDigits.format(
product1 ) + "\n" +
                                "Product 2" + "\t" + moneyFormat.format(
product2 ) + "\n" +
zero - 02 Feb 2006 15:47 GMT
> Hi,
>
[quoted text clipped - 8 lines]
>                               moneyFormat.format(
> product2 ) + "\n" +

Depending on what you're doing, the easiest way to get the currency
symbol is probably Currency:getSymbol().  Printing only two decimals can
be done with the String.format(String) method.  An example:

private static String currencySymbol = getCurrency();

private static String getCurrency()
{
  Currency curr = Currency.getInstance(Locale.getDefault());
  return curr.getSymbol();
}

void doCalculation()
{
  double amount = 3.98 / 1.8;

  outputTextArea.append("%.2f" + currencySymbol, amount);
}

One big caveat: floating point calculations are notoriously inaccurate.  
Instead of using double or float, you should consider using int or long
(and just remember that your values are off by a factor of 100), or use
BigDecimal.  When using BigDecimal, you need to use a MathContext to
specify the precision.  Again, an example:

private static String currencSymbol = getCurrency();

private static String getCurrency()
{
  Currency curr = Currency.getInstance(Locale.getDefault());
  return curr.getSymbol();
}

void doCalculation()
{
  BigDecimal amount =
     new BigDecimal("3.98").divide(new BigDecimal("1.8"));

  BigDecimal roundedAmount = amount.round(new MathContext(2));
  // implicit call to roundedAmount.toString()
  outputTextArea.append(roundedAmount + currencySymbol);
}

This may seem like a lot more work, and in fact it is.  However, if you
use doubles, you're likely to get inaccurate results.  For an example of
what may happen, look for a discussion entitled ".09 instead of .08" in
the group comp.lang.java.help


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.