I'm writing a very simple and small example class to try and show how the .getCurrencyInstance() of the NumberFormat class in Java works, but my output is looking a little funky. It seems the class is working, but when it tries to print out the actual symbols for currency (like the Japanese yen symbol or the British pound symbol) I get weird symbols instead. Is there any way to fix this? Is there something I need to import to get these symbols on my computer?
Here's my code:
import java.util.*;
import java.text.*;
public class CurrencyFormatExample
{
public static void main(String[] args)
{
double convert = 9398.9398;
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
System.out.println("American: " + currencyFormat.format(convert));
Locale swedish = new Locale("sv", "SE");
NumberFormat swedishFormat = NumberFormat.getCurrencyInstance(swedish);
System.out.println("Swedish: " + swedishFormat.format(convert));
Locale japanese = new Locale("ja", "JP");
NumberFormat japaneseFormat = NumberFormat.getCurrencyInstance(japanese);
System.out.println("Japanese: " + japaneseFormat.format(convert));
Locale russian = new Locale("ru", "RU");
NumberFormat russianFormat = NumberFormat.getCurrencyInstance(russian);
System.out.println("Russian: " + russianFormat.format(convert));
Locale british = new Locale("en", "GB");
NumberFormat britishFormat = NumberFormat.getCurrencyInstance(british);
System.out.println("British: " + britishFormat.format(convert));
}
}
Here is the output I get with this:
American: $9,398.94
Swedish: 9á398,94 kr
Japanese: ?9,399
Russian: 9á398,94 ???.
British: ú9,398.94
As you can see that's clearly not right. Any way I can remedy this?
If you're executing this code in Eclipse, adjust your Window > Preferences > General > Workspace text encoding to UTF-8
In jGRASP: "Settings" > "Font", "Charset" tab. Change "I/O Charset" to UTF-8. In the "Compiler" tab, "Flags / Args" sub-tab, for "FLAGS2" of the "Run" command, add "-Dfile.encoding=UTF-8".
The "file.encoding" change will also change the default encoding files that your program reads and writes. Unfortunately, Java does not provide a way to specify a different charset for normal file I/O and I/O from stdin, stdout, and stderr when they are connected to pipes rather than a console (most IDEs will use pipes). So if you want native encoding to be used by default within your program and still want UTF-8 for I/O to the IDE, there is no way to do it. Thus, it would not be wise for the IDE to automatically add the "file.encoding" flag, unless there is a "Just use UTF-8 for everything" setting.