Reputation: 61
after many attempts I managed to create a pattern for Float , I managed to do it by return ActivityResult and set it in the corresponding field . Now I have a problem with this TextWatcher that does not accept the pattern of Float , can anyone help me out ?
This is the TextWather
private abstract class TextChangedListener implements TextWatcher
{
public abstract void numberEntered(Float number);
@Override
public void afterTextChanged(Editable s)
{
String text = s.toString();
try
{
Float parsedFloat = Float.valueOf(text);
numberEntered(parsedFloat);
} catch (NumberFormatException e)
{
Log.w(getPackageName(), "Non si puo' parsare '" + text + "' col numero", e);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
}
This is the Float pattern
DecimalFormat dec = new DecimalFormat("###,##0.00", DecimalFormatSymbols.getInstance(Locale.ITALIAN));
Upvotes: 1
Views: 1261
Reputation: 3964
It's mostly due to the Locale issue, just define locale as English your issue will be resolved.
public double convertTwoDecimalPointAmount(double amount) {
DecimalFormat df = new DecimalFormat("0.00", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
return Double.parseDouble(df.format(amount));
}
Upvotes: 0
Reputation: 61
Thanks to all! I resolve my problem with this method!
private void decimalFormatWithSymbol() {
dec = new DecimalFormat("##0.00", DecimalFormatSymbols.getInstance(Locale.ITALIAN));
dfs = new DecimalFormatSymbols();
dfs.setDecimalSeparator('.');
dec.setDecimalFormatSymbols(dfs);
}
Upvotes: 1
Reputation: 15165
If you want to give some decimal numbers to float
, convert it to double
:
float f = 90;
DecimalFormat decimal2 = new DecimalFormat("#.##"); // gives 2 numbers after '.'
double dec = Double.valueOf(decimal2.format(f));
Above code will gives you the output:
90.00
Upvotes: 0