Decimal Format separator - android

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.

Related

How to make EditText, with numberDecimal Input Type, display the decimal Separator as "," rather than "." according to application Locale?

I have an android application that shall correspond to French localization, where the Decimal Format for numbers shall have the Decimal Separator as "," rather than ".",
I need to let the EditText by default separate the decimal number with "," when entering a number in the EditText, where adding a textWatcher on every numeric field isn't an option, as there are many numeric fields, and many locales in which the user may run the application accordingly, and accordingly adding a text watcher for each locale isn't an option,
What am trying to do is to set the application DecimalFormatSymbols on login according to logged in culture, and accordingly all numeric EditText controls shall append the specified DecimalSeparator according to what specified after login via Example: using the method DecimalFormatSymbols.setDecimalSeparator(','),
Is that doable,
Please Advise!
Workaround
char separator = DecimalFormatSymbols.getInstance().getDecimalSeparator();
editText.setKeyListener(DigitsKeyListener.getInstance("0123456789" + separator));

format edittext input price format

When we work with the currency we need to use ',' separator in appropriate places . For example 1500 as 1,500.
I have a issue. My applications require formatting the EditText's value while typing. I.E., a number that needs to be formatted with decimal and thousands separators. Example, I input 123456789, the EditText display 123.456.789.
How to I do this issue ? Thank all.

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.

How to return 1 decimal place for the answer of this calculated Label in Adobe Flash Builder

I am writing code in Adobe Flash Builder for an Android application. I have written my code to do some math and return the answer to a label field. I would like to know how do I return this answer to show only 1 spot after the decimal. Here is the code
lblAnswer.text = String(Number ((sldrABSL.value) + 46.7)/28.7);
If there are any suggestions please let me know.
If I understand this correctly, you have a string representing a number, which you want to be presented with only one decimal.
Fist of all, you'd have to convert the numeric value of the string to a double:
String stringOfNumber = "100.1233123";
Double number = Double.valueOf(stringOfNumber);
Secondly, you'd have to establish the format of which to represent the double (number of decimals):
DecimalFormat oneDigit = new DecimalFormat("#,##0.0");
Set the digit to a (i.e) TextView:
myTextView.setText("" + oneDigit.format(number));
I think this should work. Is this kind of what you were asking?
Edit: Not super certain as to how to set it to a textview, but in java, printing it to screen works like this:
System.out.println(oneDigit.format(number));
Edit2: Oh, and you'll need to this import:
import java.text.DecimalFormat;
Edit3:
TextView.setText("" + oneDigit.format(number));
works fine for me.
The decimal places uses the tofixed property. It must be added at the end of the specified number that needs to contain the decimal place.
'lblAnswer.text = String(Number ((sldrABSL.value) + 46.7)/28.7).toFixed(1);
The one specifies the number of decimal places that are used.

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