Solved

Currency Symbol

Posted on 2003-11-06
7
700 Views
Last Modified: 2013-11-18
I am using JDK1.4. When run the following code
> Locale sg = new Locale("en", "SG");
> System.out.println(sg.getLanguage() + "   " + sg.getCountry() + "   " + NumberFormat.getCurrencyInstance(sg).format(1000000));
I get
> en   SG   SGD1,000,000.00

Actually what I wanted is S$1,000,000.00
Is it possible to configure it to use S$ instead of SGD. I understand that currency code is used when the symbol is not available. Is there a way to plugin the setting I want to specific locale?
0
Comment
Question by:RayLeong
  • 4
  • 2
7 Comments
 
LVL 15

Expert Comment

by:jimmack
ID: 9694710
I haven't used it personally, so I'm just suggesting this ;-)

java.util.Currency may be what you want.

It has two methods to retrieve the currency symbols.
0
 
LVL 3

Accepted Solution

by:
savalou earned 100 total points
ID: 9695835
If you do NumberFormat.getAvailableLocales you get an array of locales supported by NumberFormat.  In my implementation of J2SDK1.4 I have 134 locales available and Singapore (SG) isn't one of them.  So first of all NumberFormat doesn't do anything special for your country case.  So you might as well get your currency symbol from Currency.

However, the currency information comes out of locale data and the Currency class is more or less a convenience class for this.  When it doesn't find a currency symbol, it just returns the currency code.  For Singapore, it doesn't find a currency symbol.   You can't override this in code.

You could lobby Sun that Singapore is an important economic zone and it's currency should be given better treatment.  In the meantime, you could create a new DecimalFormatSymbol, set its currency code, use a Decimal format to format your number.
        Locale sg = new Locale("en", "SG");
        DecimalFormatSymbols sgsymbols = new DecimalFormatSymbols(sg);
        sgsymbols.setCurrencySymbol("S$");
        DecimalFormat f = new DecimalFormat("\u00A4###,###.00", sgsymbols);
        System.out.println(f.format(1000000));

But in the end do you gain any generality?  Might as well slap a "S$" in there?
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9697008
Well I tried out the Currency class, with quite disappointing results.  Most symbols were just codes.  There were a few that were correct (eg. the sterling symbol was correct for UK pounds), however the US Dollar symbol was not shown.  I just got USD.

Here is the code I used.  It might be worth trying on your system to see what you get (I had 134 locales, just like savalou).

Other than that I'd go along with savlou's "Campaign for the recognition of the Singapore Dollar" ;-) and implement his suggestion.

If you do run the following code, I'd be interested to know if you got the symbol (or even the locale).

import java.util.*;

public class CurrencyLocales
{
    public static void main(String[] args)
    {
        Locale[] locales = Locale.getAvailableLocales();
        System.out.println("Available locales = " + locales.length);
       
        for (int i = 0; i < locales.length; i++)
        {
            System.out.print("Locale = " + locales[i].toString());
            try
            {
                Currency currency = Currency.getInstance(locales[i]);
                System.out.println(" Currency = " + currency.getSymbol());
            }
            catch (IllegalArgumentException iae)
            {
                System.out.println(" Currency = Invalid locale");
            }
        }
    }
}
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:RayLeong
ID: 9698768
Other than programming, is there any way to plugin a class, property file to overwrite the default currency symbol used for a particular locale. Is the Currency or Locale design to take in other class, property file created by developers.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9702421
I've quickly searched around a few times today, but I can't find an easy way to do this.  I think savalou has provided the best answer for this problem.

Sorry.

Jim.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9730445
It's never nice to receive a grade C, so savalou, I sympathise.  However, RayLeong, this is the first time I have actually seen a grade C given appropriately.  Thank you for reading (and understanding ;-)) the grading guidelines ;-)
0
 

Author Comment

by:RayLeong
ID: 9735970
Savalou had gave a sugguestion which is quite valid. The problem lies in my question. I don't think my question has an answer, so I really should not mislead the public into thinking that this question is closed with a proper solution. Savalou, no offends for the C grade though.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
count7 challenge 12 70
json example 39 114
where is session ID cache stored 1 39
base64 decode encode 12 97
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now