• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Currency vs NumberFormat

 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We do use NumberFormat class to format currency according to locale,then what is for Currency class ?

Thanks
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read API but couldn't figure out when to use Currency instead of NumberFormat,Please elaborate.Thanks
 
Winston Gutkowski
Bartender
Pie
Posts: 10508
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49808
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
consider the % tags for displaying such values.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic