0

All- I have an EditText field where the user enters a currency value (eg. "120.45"). I need to take that value and use it in an equation. The problem is, the decimal point gets ignored so instead of multiplying 120.45 by 2 it multiples 12,045 by 2. Is there any way to make the equation see this value as "120.45". Here is my XML:

<EditText android:id="@+id/edit_bill"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"               
    android:layout_toRightOf="@id/view_dollar_sign" 
    android:layout_alignBaseline="@id/view_dollar_sign" 
    android:padding="5dp"
    android:singleLine="true"
    android:hint="@string/edit_bill"
    android:digits="0123456789."
    android:maxLength="6" 
    android:gravity="right"
    android:layout_alignBottom="@id/view_dollar_sign"
     />

Here is my code:

EditText text = (EditText)findViewById(R.id.edit_bill);             
    String value = text.getText().toString();       
    if(!value.contains("."))
        value = "."+value;
    float bill = Float.parseFloat(value);

Thanks in advance!

4

2 回答 2

1

You can parse your strings to floats or doubles first, e.g.

    x = Double.parseDouble(editTextName.getText().toString());

Then perform operations as necessary!

Afterwards you could could change it back to a output TextView like this:

 answerTextView.setText("" + x);
于 2012-06-01T16:57:49.857 回答
0

remove if condition from yourcode

EditText text = (EditText)findViewById(R.id.edit_bill);             
    String value = text.getText().toString();       
    // if(!value.contains("."))   <---REMOVE THESE LINES
       //  value = "."+value;    <---REMOVE THESE LINES
    float bill = Float.parseFloat(value);
于 2012-06-01T16:57:58.910 回答