2
Double value = Double.valueOf(temp.getText().toString());

Let's say editText temp is 32 Celsius. I only want to get 32 instead of "32 Celsius" so how would I be able to do this? Is this possible?

I'm a newbie with Android Studio.

Marcella Ruiz
  • 323
  • 1
  • 3
  • 15

2 Answers2

1

The simplest way that comes into my mind is

Double value = Double.valueOf(temp.getText().toString().split(" ")[0]);

which splits the string using the space and only takes the first value in the generated array.

This works if the user enters something like "[any_number] [any other string, even more words]".
i.e.: "32 Celsius", "952 years ago", "4 pieces of cake", ...

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
1

use android:inputType="number" in your EditText layout

adnbsr
  • 635
  • 4
  • 13