Android NumberFormat Currency - android

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

Related

how to show a String variable with color in alertDialog.setMessage() in android

I want my alertDialog look like this
My API is 26, and this is my code so far
alertDialog.setTitle("Warning");
alertDialog.setMessage(Html.fromHtml("Are you sure"+"\n"+"\n"+"<font color='#00bfff'><br><br><b>Est. Total Cost : $ 9.00 </b></font>",Build.VERSION.SDK_INT));
As you can see I just put 9.00 directly in the String.
Right now, I want to make 9.00 a string variable called estCost, which can be changed based on the calculation in my code.
How can I display the "string variable with the color" estCost in the alertDialog.setMessage() ?
I have tried following code
String estCost = calculate(10) //calculate method will return a double in string.
alertDialog.setTitle("Warning");
alertDialog.setMessage(Html.fromHtml("Are you sure"+"\n"+"\n"+"<font color='#00bfff'><br><br><b>Est. Total Cost : $ <var> estCost </var></b></font>",Build.VERSION.SDK_INT));
But it doesn't work. Please help me ! Thanks.
Use spannable text
Spannable myText = new SpannableString("are u sure.....");
myText.setSpan(new ForegroundColorSpan(Color.RED),7,10,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
alertDialog.setMessage(myText);
You need to concat the value using concatenation operator "+"
alertDialog.setMessage(Html.fromHtml("Are you sure"+"\n"+"\n"+"<font color='#00bfff'><br><br><b>Est. Total Cost : $ <var>"+ estCost+" </var></b></font>",Build.VERSION.SDK_INT));
You need to concat the font so use + before the font tag and after the font tag that's all.

Decimal Format separator

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.

convert a double scientific notation to decimal notation in JNI

I have jdouble(double) value coming in from my activity which when printed gives in scientific notation i.e in E format. Now i want to show it in decimal format and truncate it so i used "%.2f" in my format specifier to do that. But weirdly the "%f" format specifier is showing 0.000000 as the final value. please suggest some advice.
P.S I'm doing this on the native side of android.
In order to show decimals instead of E format notation and truncate a number you can use NumberFormat:
NumberFormat formatter = new DecimalFormat("#0.000");
An then, where you want to use your number:
formatter.format(yournumber)
e.g.:
Toast.makeText(context, "Truncated number: " + formatter.format(yournumber), Toast.LENGTH_LONG).show();
In this case, your number will be shown with 3 decimals.
Add as many zeros as decimals you want to show.

Android select currency

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"

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