Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting number to currency?? 1

Status
Not open for further replies.

MissouriTiger

Programmer
Oct 10, 2000
185
US
Hi, it's me again. I'm trying to format a numeric output to currency, and once again I don't understand why my syntax isn't working. I'm trying to use the getCurrencyInstance method, which I found in the book "Java in a Nutshell". Here's my statement:

txaResults.append("Free Eve/Weekend\t" + getCurrencyInstance(dblPlanChrgs[0][0])

I also have an import statement:

import java.text.*;

Could somebody please explain how to do this?


 
getCurrencyInstance is a static method of the class NumberFormat (see the URL in the JDK documentation below - you can also download this documentation). So you'll first have to say to Java that it has to use this class. If the method was not static, you'd even have to have an instance of the class.

As it is static, you can use the following line to append to your (existing, i.e. instantiated via new!) StringBuffer:

txaResults.append("Free Eve/Weekend\t" + NumberFormat.getCurrencyInstance().format(dblPlanChrgs[0][0]));

Hope this helps...

Documentation: Download:
allow thyself to be the spark that lights the fire
haslo@haslo.ch - www.haslo.ch​
 
Thanks for the help and for the link. I already looked at it. I keep going to the JDK docs, but all they do is confuse me. If you want to see a perfect example of why the documentation throws me off, go to the same URL you pasted above, and you will see this:

myString = NumberFormat.getInstance().format(myNumber);

Why on earth are they using a string? As soon as I saw this I thought to myself "Oh, I guess this isn't what I'm looking for. This is for strings." But then I look again and it says it's for formatting numbers, so why are they using a string in their example? I wonder, "Does this mean I have to convert it to a string first?"

Anyway, thanks again for your help and patience.




 
:) - it simply because double values are stored in no particular format, they're just numbers in their binary representation (double precision floating point, according to some IEEE standard). And therefore they use pretty much less memory. If you format them, you have to do this in a String.
allow thyself to be the spark that lights the fire
haslo@haslo.ch - www.haslo.ch​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top