I saw in the forum that others have the same problem, but no one explains how he solved.
I have a TextView in my Activity, with a string in currency format
For example, if the user has the phone set to U.S. English language, and yet want to display currency in € or more, how can I do?
Double value_tot = Double.valueOf(totale);
NumberFormat formatter = NumberFormat.getCurrencyInstance();
String super_totale = formatter.format(value_tot );
aTotale.setText(super_totale);
You can configure the currency to use in the NumberFormat class, like this:
NumberFormat nf = NumberFormat.getCurrencyInstance();
nf.setCurrency(Currency.getInstance("EUR"));
Then nf.format(123.5) will produce "€123.50"
Related
I have ran into a little tricky thing here.
The thing is that I want to get the Locale Currency of the User, so I do this:
final Locale currentLocale = getResources().getConfiguration().locale;
final Currency currency = Currency.getInstance(currentLocale);
mCurrencyCode = currency.getCurrencyCode();
Which gives me: AUD as for Australia
The problem? Well, I am in New Zealand and the Currency Code is NZD
How could I get the right Currency Code?
Cheers
I have a problem with formatting decimal values for localizing in Spanish (and other European languages, as well) when the decimal separator for Spain comes out as a comma ,.
How can I get the decimal separator as dot .?
Here is the code I used for formatting a decimal number
DecimalFormat twoDecimalForm = null;
twoDecimalForm = new DecimalFormat("#.##");
DecimalFormatSymbols.getInstance().setDecimalSeparator('.');
String valueInDecimal = twoDecimalForm.format(0.005 * progressValue);
//Exception here
Log.d(TAG,"valueInDecimal" + Double.valueOf(valueInDecimal));
I am getting a comma , instead of of a dot .
A workaround can be replace the comma with ..
But is there any other solution?
If you getting data from web then ask your web developer to pass data as per your requirement, then your problem gets resolved or you can go with replace option.
How can I convert the Currency to float?
for example € 10,10 to 10.10
I tried it but no good.
float f = Float.valueOf(tv.getText().toString()).floatValue();
String temp = tv.getText().toString().replace.(",",".");
tv.setText(""+temp);
Of course it's no good, because you have to remove the character of the currency first (€, $...). You could use String.replace("€", "") for example, and then Float.parseFloat(yourString).
One suggestion, do not allow the user to type in any character but numbers so parsing does not fail
In the EditText for input
android:inputType="numberDecimal"
http://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType
I have a question, I am doing an App that is a kind of bill however I don't know how to express in a textview for example:
1120 as $1.120 or something like this.
Thanks in advance
numberformat is your solution :
Test code :
int = 1236;
NumberFormat nf = NumberFormat.getInstance(Locale.US);
Log.i("test", nf.format(nb));
show output :
1,236
There are a number of solutions to format numbers in Android which of course depend on the nature of your app and your requirements.
Because you are using a money value in your example, I would look into Big Decimal at has numerous precision and rounding methods.
Here's a little example for you, note that it is not localised!
BigDecimal theAmount = new BigDecimal(int);
DecimalFormat decimalFormat = new DecimalFormat("$#,##0.00;-$#,##0.00");
// ^ This is set manually, you could use a localised format to have Android set the
// values based on locale like this: DecimalFormat.getInstance();
decimalFormat.setDecimalSeparatorAlwaysShown(true);
decimalFormat.setMinimumFractionDigits(3);
decimalFormat.setMaximumFractionDigits(3);
// ^ There are a lot of methods to check out here!
TextView yourTextView.setText(decimalFormat.format(theAmount.divide(new BigDecimal(1000))));
Good luck!
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