Money Format + Decimal Place Format

Discussion in 'Java' started by shannon, Feb 1, 2006.

  1. shannon

    shannon Guest

    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" +
     
    shannon, Feb 1, 2006
    #1
    1. Advertising

  2. shannon

    zero Guest

    "shannon" <> wrote in
    news::

    > 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" +
    >
    >


    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
     
    zero, Feb 2, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Artek
    Replies:
    3
    Views:
    2,766
    Steven Cheng[MSFT]
    Feb 14, 2004
  2. tshad

    Money format

    tshad, Jun 9, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    7,754
    lionstar
    Nov 3, 2009
  3. shannon
    Replies:
    0
    Views:
    574
    shannon
    Feb 1, 2006
  4. Sam
    Replies:
    1
    Views:
    8,844
    Prnda76
    Feb 11, 2010
  5. Duwayne
    Replies:
    1
    Views:
    121
    Lasse Reichstein Nielsen
    Aug 27, 2003
Loading...

Share This Page