| 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: 1403
|
|
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: 4734
|
|
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?
|
 |
Jelle Klap
Bartender
Joined: Mar 10, 2008
Posts: 1403
|
|
|
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
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
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 7 API documentation
Scala Notes - My blog about Scala
|
 |
Enkita mody
Ranch Hand
Joined: Aug 06, 2012
Posts: 333
|
|
Thanks.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32604
|
|
|
consider the % tags for displaying such values.
|
 |
 |
|
|
subject: Currency vs NumberFormat
|
|
|