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
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.
You can try the following:
This prints:
No need to reinvent the wheel.
DecimalFormat
comes with currency support:This also comes with full locale support by optionally passing in a
Locale
:Here's a test:
Output:
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.
Output: