How to format a number as currency using only currency code? - android

My business problem is as follows: I have a currency code (e.g. "USD") and a number (either float or integer, I can parse either way) and I need to format this number to a currency string using the currency code. For example, 124.3222 and "USD" should create the string "$124.32".
I can create a Currency instance using Currency.getInstance(String), which gives me the symbol and some other information. However, it does not provide any way to format the number as a string. On the other side of the problem, NumberFormat contains several static methods that return a NumberFormat instance capable of doing what I need (e.g. NumberFormat.getCurrencyInstance()).
The problem with NumberFormat is they are all centered around either the default locale, or a passed-in Locale. Given the nature of this app, locale is meaningless. There is no correlation between locale and the currency I need to format as a string. I can use neither the default locale, nor do I have any sort of locale identifier ISO value. All I have is the currency code.
It seems like I'm so close yet so far. There is (in my opinion) an odd disconnect between Currency and the NumberFormat.getCurrencyInstance. Currency can parse a currency code but not locale and cannot format strings, while Locale cannot parse a currency code but can format strings. Am I missing something here?
Edit: I should clarify that I can manually format the number using the symbol and decimal count provided by Currency instance, but I don't see how to figure out where to put the symbol in the string. At any rate, it seems like I should use the built-in currency formatting whenever possible.

I discovered there is a setCurrency() method on NumberFormat. So what I am doing is calling NumberFormat.getCurrencyInstance() so I get a formatter for my locale, and then calling setCurrency() on it for the currency I need to format. This seems to do what I need.

Related

String resource and decimal format

I know I can format a number to a String with decimal separators like:
NumberFormat.getInstance().format(1000)
and receive the String 1,000.
How can I apply this to formated Strings from strings.xml resources?
Example:
<string name="number_of_results">%1$d results</string>
Calling
getString(R.string.number_of_results, 1000)
will return 1000 results. How can I get 1,000 results?
Android string resources are not that smart. If you need sophisticated formatting you should do it yourself, and then insert it as a string:
<string name="number_of_results">%1$s results</string>
And then
getString(R.string.number_of_results, NumberFormat.getInstance().format(1000));
The formatting libraries usually provide you interface for the locale setup, so your numbers will be properly localized, no worries here.

Type conversion for multilanguage java

My app supports multi language, i have a problem when dealing with numericals. Suppose my selected language is hindi and I perform some calculation operation on the value taken from edit text. I had to format the float value after the calculation (say like my resulting calculation gives me 12.345678) i will use the decimal format class to get only 12.34 (like only two digits after decimal point). Till here is fine, but i wanted the resulting string from decimalformat class( i.e: 12.34) to be back as a float. Here the crash happens stating NumberFormatException. Because the resulting 12.34 comes out as a hindi 12.34
How can i format the resulting calcution (i.e: 12.3456) to 12.34 without converting it to string(i.e: decimalFormat.format(value)).

How to prevent default language to be forced when using string format

So I Have a TextView in my calculator app that I eventually display the results in it ...
It works ... but recently during the testing I found out that if The Default Language of That device is set to a non-english language (for example arabic or farsi) numbers in TextView get to be shown in that specific language (not english) and in a completely different format !!
I used this code to generate the result
result = String.format("%.4f", mResultBeforeFormatting);
resultTextView.settext(result);
also to note is that if I set the TextView with a hard coded string the issue doesn't happen
resultTextView.settext("343");
The formats used by the String.format method will by default be the formats specified by the device's default locale.
If you want to force the use of a specific locale, use the String.format method that accepts a locale parameter.
For example:
result = String.format(Locale.ENGLISH, "%.4f", mResultBeforeFormatting);

Android Currency Class - How Do you Get the Display Name for the Currency?

I can easily set a Currency using it's ISO 4217 code, such as "USD". This will allow me to grab the dollar symbol using .getSymbol(), however, how the heck do I get the display name "dollar" out of the Currency?
I have a spinner, I'd like to populate with the names of currencies like "dollar", "euro", "yen", etc. however, I can't extract these from the ISO 4217 codes. Shouldn't this be quite easy?
Please note, I can't use simple string arrays to solve this. That's because I grab the default locale and add it's currency to the spinner mentioned above. This is the reason I need to be able to grab currency display names dynamically, I don't know all the currencies users may use.
Thanks!
Refer to this question.
Can I get a text description of an ISO currency code in Java?
It might answer your question. There are many answers on that page.

String.format uses comma instead of point

My app is working on many devices without problems so far. But now I got my new Galaxy Tab with Android 3.2 where it crashes all the time. I found out that the problem was a float in an EditText.
I am using myEditText.setText(String.format("%.1f", fMyFloat)); to put the float in the EditText. But somehow the float on my 3.2 Galaxy Tab is generated with a comma instead of a point. When I read the EditText back the app crashes of course, telling me that this is no valid float because of the comma...
What is going wrong here?
Convert float to string..
From the documentation of String.format:
String.format(String format, Object... args)
Returns a localized formatted string, using the supplied format and arguments, using the user's default locale.
The quoted text above means that the output of String.format will match the default locale the user uses.
As an example a comma would be used as the decimal-point-delimiter if it's a user using Swedish locale, but a dot if it's using an American.
If you'd like to force what locale is going to be used, use the overload of String.format that accepts three parameters:
String.format (Locale locale, String format, Object... args)
Convert string to float..
Parsing an arbitrary string into a float using the default locale is quite easy, all you need to do is to use DecimalFormat.parse.
Then use .parse to get a Number and call floatValue on this returned object.
Your format call on your Galaxy Tab uses some default Locale which in turn uses , for floats. You could use String.format(Locale,String,...) version with specific locale to make things work.
Or you should've used same locale both for parsing and formatting the number. So you should probably go with NumberFormat to format and parse your floats.
String.format uses the locale you are in. You should do something like this if you want a dot:
NumberFormat formatter = NumberFormat.getInstance(Locale.US);
myEditText.setText(formatter.format(fMyFloat);
Have a look into NumberFormat for more formatting options
Use below code it's works for me:
NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
DecimalFormat df = (DecimalFormat)nf;
df.applyPattern(pattern);
String output = df.format(value);
System.out.println(pattern + " " + output + " " + loc.toString());
Summing up previous answers, an easy way to have the dot instead of the comma in all country, is this:
myEditText.setText(Locale.CANADA, String.format("%.1f", fMyFloat));
And you will have your String formatted with the dot

Categories

Resources