Android: Commas in Data Entry
I have users in Europe (Spain/Germany) that are somehow getting commas in their data entry. With over 100,000 users, only some in Europe are having issues. None of my American or other European customers are reporting any problems.
One customer that is helping has made sure to enter the number using only a decimal point and I still have the problem.
The field in the Layout is:
<EditText
android:id="@+id/xmlPrice"
android:textSize="17sp"
android:layout_height="44dp"
android:layout_width="105dp"
android:gravity="right"
android:numeric="decimal"
android:maxLength="7"
android:inputType="phone"
android:digits="0123456789."
android:selectAllOnFocus="true"
android:imeOptions="actionNext"
android:hint="0.00" />
My class code is as follows:
EditText etPrice ;
etPrice = (EditText) findViewById(R.id.xmlPrice);
String v_price = etPrice.getText().toString().trim();
double d_price ;
try {
d_price = Double.parseDouble( v_price );
} catch(NumberFormatException ex )
{
d_price = -1 ;
}
DecimalFormat fmtval = new DecimalFormat( "0.00" );
v_price = fmtval.format( d_price ) ;
mDbHelper.insertRec( v_id, v_price );
When I actually get a copy of their database, sure enough, the data has commas in it. The dbHelper simply inserts the data with no additional checks, as the code upfront should properly verify correct data entry.
I cannot reproduce the problem in either the Emulator or m开发者_如何学Cy 2 test devices.
Any ideas?
It likely has to do with the users default locale. From the documentation of Locale
there is a subsection labeled Be wary of the default locale
, with a portion that reads:
For example, if you're formatting integers some locales will use non-ASCII decimal digits. As another example, if you're formatting floating-point numbers some locales will use ',' as the decimal point and '.' for digit grouping.
You create your DecimalFormat
object using just a pattern, which by the documentation says that it uses the users default locale.
You can either force a locale, by using NumberFormat.getCurrencyInstance(Locale)
static method (not recommended), or you can provide support for other locales (recommended).
精彩评论