Question

I'm having an issue when I get a whole number from an EditText and try to change that to a decimal so I can use it for calculations. Could someone explain how to do this?

For Example. if someone was to enter 120 into the EditText and I got the integer from it, how would I then change that integer of 120 into 1.20 and continue calculations with it?

Thanks!!

Was it helpful?

Solution

EditText myEditText = (EditText)findViewById(R.id.YOUR_EDIT_TEXT_ID);
String numberAsString = myEditText.getText().toString();

double myDecimal;
try {
    myDecimal = Double.parseDouble(numberAsString);
    if (myDecimal >= 10)
    {
        int digits = 1 + (int)Math.floor(Math.log10(myDecimal));
        myDecimal = myDecimal / ((Math.pow((double)10, ((double)digits) - 1)));
        System.out.println(myDecimal);
    }
} catch (NumberFormatException e) {
    //handle exeption
    e.printStackTrace();
}

OTHER TIPS

You need to get the contents from your EditText as a string, and from there you can parse it into an integer. This is done like so:

 int wholeNum = Integer.parseInt(yourEditText.getText().toString());
int three = Integer.parse("3");

I know you can use this to parse a string into an integer, there is probably a parse method in double aswell!

Check this also : Convert a String to Double - Java

+1 to anthony for answering 1 minute before me haha

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top