File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Currency vs NumberFormat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Currency vs NumberFormat " Watch "Currency vs NumberFormat " New topic
Author

Currency vs NumberFormat

Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

We do use NumberFormat class to format currency according to locale,then what is for Currency class ?

Thanks


OCA7
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1836
    
    7

They serve two very different purposes, as is detailed in their respective API descriptions.
In fact NumberFormat's subclass DecimalFormat uses the Currency class to determine the symbol to use when formatting a value.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

I read API but couldn't figure out when to use Currency instead of NumberFormat,Please elaborate.Thanks
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

ankita modi. wrote:We do use NumberFormat class to format currency according to locale,then what is for Currency class ?

There is a lot more to currencies than just formatting. Just for example, every currency has:
1. A symbol: eg, "$"
2. A code: eg, "USD"
3. (Generally, but not always) A country.
4. A format (basically, this is the number of digits used after the decimal point for the smallest unit).
and the Java Currency class holds all that information according to the ISO 4217 specification.

One thing I notice it doesn't hold is whether the symbol is used as a prefix or a suffix which, I have to admit, is quite surprising.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1836
    
    7

That should be fairly straightforward. If you look at the currency API, does it offer you a means of formatting a Number (or primitive data types) as String value, or parsing a String value as a Number?
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

Note that the Currency class is designed so that there is never more than one Currency instance for any given currency. Therefore, there is no public constructor. As demonstrated in the previous code example, you obtain a Currency instance using the getInstance methods.

source
Could anyone explain this ?
Thanks
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14433
    
  23

What exactly about those sentences do you not understand?

It explains that for every currency (for example US dollars, Euro, etc.) there is exactly one Currency object. The class Currency has no public constructors, so that you cannot create a second Currency object for an existing currency. Instead of a public constructor, there's a getInstance() factory method that returns the Currency object for the currency that you pass to the getInstance() method.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
consider the % tags for displaying such values.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Currency vs NumberFormat