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!

Brad Werth
  • 17,411
  • 10
  • 63
  • 88
ninge
  • 1,592
  • 1
  • 20
  • 40
  • "the decimal point gets ignored" by what? Are you trying to parse it to an Integer? Do you have a special inputType set on your EditText? Post the XML for your EditText and explain how you're retrieving the value. – Kevin Coppock Jun 01 '12 at 16:58
  • refer this previous post, http://stackoverflow.com/questions/4854360/entering-decimal-values-in-edittext – Aerrow Jun 01 '12 at 16:58

2 Answers2

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);
Jaypoulz
  • 110
  • 11
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);
Chintan Raghwani
  • 3,370
  • 4
  • 22
  • 33