Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Money Format + Decimal Place Format

Reply
Thread Tools

Money Format + Decimal Place Format

 
 
shannon
Guest
Posts: n/a
 
      02-01-2006
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" +

 
Reply With Quote
 
 
 
 
zero
Guest
Posts: n/a
 
      02-02-2006
"shannon" <> wrote in
news: oups.com:

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.NET FormView Money Data Type Binding Problem with 2 Decimal Points.. Sam ASP .Net 1 02-11-2010 02:22 PM
Money? What money? It never existed. joevan Computer Support 7 10-13-2008 12:05 PM
Money Format + Decimal Place Format shannon Java 0 02-01-2006 10:02 PM
Problems with money, decimal numbers after migration to Framework 1.1 Artek ASP .Net 3 02-14-2004 07:01 AM
Forcing numbers to show with 2 decimal places for money types Duwayne Javascript 1 08-27-2003 10:57 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57