16

There is some existing code of the follow form which is used for format numerical values:

String.format( pattern, value )

Note that I cannot change the code itself - I can only change the format pattern supplied to the code.

What is the format pattern to output a currency symbol for the default locale? Essentially, I want to achieve the following output:

String.format( "...", 123 )  => $ 123
11
  • Maybe worth look at NumberFormat. Commented May 31, 2012 at 2:56
  • edited to mention I cannot change the code Commented May 31, 2012 at 4:06
  • @MarcPolizzi: you can't change the code, but you can change the format string (presumably specified elsewhere) - is that right? Commented May 31, 2012 at 4:18
  • @Greg: sorry forgot to mention yes only the pattern itself. Commented May 31, 2012 at 4:25
  • I think it is not possible to change the currency symbol automatically. Code must be placed in hard-code. Commented May 31, 2012 at 5:11

5 Answers 5

18

No need to reinvent the wheel. DecimalFormat comes with currency support:

String output = DecimalFormat.getCurrencyInstance().format(123.45);

This also comes with full locale support by optionally passing in a Locale:

String output = DecimalFormat.getCurrencyInstance(Locale.GERMANY).format( 123.45);

Here's a test:

System.out.println(DecimalFormat.getCurrencyInstance().format( 123.45) );
System.out.println(DecimalFormat.getCurrencyInstance(Locale.GERMANY).format( 123.45)) ;

Output:

$123.45
123,45 €
Sign up to request clarification or add additional context in comments.

Comments

5

You can try the following:

public static void main(String[] args) {
    System.out.println(String.format(" %d \u20AC", 123)); // %d for integer
    System.out.println(String.format(" %.2f \u20AC", 123.10)); // %f for floats
}

This prints:

123 €
123.10 €

Comments

4

With the constraints you have given, I think it is impossible to achieve it. To get to the current Locale's currency symbol, you'll need a minimum of code.

If you have absolutely no means to add code to the program, you'd best use the established symbol for "currency" (¤). It was established for this exact purpose, to symbolize currency absent any more specific symbol.

If you can't change the given code, but add code to the project as a whole, you can use that to find out the symbol best used. Once you have it, you can use it to create a pattern for the existing code to use for formatting.

If you can find out in which Locale the original program will run in next, you could write a assistant program that uses that setting to fill your configuration.

Comments

3

If there is no default Locale available, we can go with setting the currency symbol using unicode and decimal formatting. As in the below code:

For e.g. Setting the Indian currency symbol and formatting the value. This will work without user making changes in the settings.

Locale locale = new Locale("en","IN");
DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getCurrencyInstance(locale);
DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(locale);
dfs.setCurrencySymbol("\u20B9");
decimalFormat.setDecimalFormatSymbols(dfs);
System.out.println(decimalFormat.format(12324.13));

Output:

₹12,324.13

Comments

-3
    String formatstring=String.format("$%s", 123);
    System.out.println(formatstring);

2 Comments

Does not seems to print a EURO symbol if the local of my JVM is French; still display $
ohhh sorry . I thought you want to print $ .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.