Reputation: 73
I'm using a TextWatcher for editing values while entering in EditText here is my TextWatcher
public class NumberTextWatcher implements TextWatcher {
private DecimalFormat df;
private DecimalFormat dfnd;
private boolean hasFractionalPart;
private EditText et;
public NumberTextWatcher(EditText et)
{
df = new DecimalFormat("#,###");
df.setDecimalSeparatorAlwaysShown(true);
dfnd = new DecimalFormat("#,###");
this.et = et;
hasFractionalPart = false;
}
@SuppressWarnings("unused")
private static final String TAG = "NumberTextWatcher";
@Override
public void afterTextChanged(Editable s)
{
et.removeTextChangedListener(this);
try {
int inilen, endlen;
inilen = et.getText().length();
String v = s.toString().replace(String.valueOf(df.getDecimalFormatSymbols().getGroupingSeparator()), "");
Number n = df.parse(v);
int cp = et.getSelectionStart();
if (hasFractionalPart) {
et.setText(df.format(n));
} else {
et.setText(dfnd.format(n));
}
endlen = et.getText().length();
int sel = (cp + (endlen - inilen));
if (sel > 0 && sel <= et.getText().length()) {
et.setSelection(sel);
} else {
// place cursor at the end?
et.setSelection(et.getText().length() - 1);
}
} catch (NumberFormatException nfe) {
// do nothing?
} catch (ParseException e) {
// do nothing?
}
et.addTextChangedListener(this);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
if (s.toString().contains(String.valueOf(df.getDecimalFormatSymbols().getDecimalSeparator())))
{
hasFractionalPart = true;
} else {
hasFractionalPart = false;
}
}
after that, I try to Parse the values to doubles using this code :
String amount1 = amount.getText().toString().replaceAll("[^\\d]", "");
String duration1 = duration.getText().toString().replaceAll("[^\\d]", "");
String interest1 = interest.getText().toString().replaceAll("[^\\d]", "");
But the problem i have is that when Device Default language is not English, it can parse the string to Doubles, so i think i set US Locale for editTexts! is this possible? if not, what should i do in order to be able to parse values to doubles?
DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Locale.US);
Upvotes: 0
Views: 1255
Reputation: 4338
When you have control over the back-end I find it easiest to deal with what language you are expecting and have done extensive tests on. Therefore, when handling file IO on files that I have internally, I always call the call that you have at the end of your comment there before any file creation code is performed. In my case it would be
final DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getInstance(Locale.ENGLISH); //Format our data to two decimal places for brightness change.
String stringFormat = "#0.00";
decimalFormat.applyPattern(stringFormat);
decimalFormat.format(dataString);
Then, no matter what language the device is actually set to, you are always dealing with the locale that you are accustomed to. And this will aide in handling other languages that might use different number formats for instance in this case since I was handling numerics. Since you are dealing with doubles you might be approaching this numeric translation issue. But if you are dealing with inputs from an EditText, this particular approach that was only on my back-end might not be applicable. But I thought communicating my approach might still be somewhat helpful; hopefully anyways. Doesn't hurt to share.
Upvotes: 1