Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a Edit Text to a double android

I have 2 edit texts and because I want to make them decimals I can not make them integers so I have to turn them into a double. I have this code but in the n2Var get text I have an error.

    mul = (Button) findViewById(R.id.button);
    n1 = (EditText)findViewById(R.id.editText);
    n2 = (EditText)findViewById(R.id.editText2);
    ans = (EditText)findViewById(R.id.TextView10);

    double n1Var = Double.parseDouble(n1.getText().toString());
    double n2Var = Double.parseDouble(n2,getText().toString());

Thanks for any help!

like image 877
somone playsmc Avatar asked Feb 17 '16 00:02

somone playsmc


People also ask

How do I turn off edit text on Android?

To disable an EditText while keeping this properties, just use UI. setReadOnly(myEditText, true) from this library. If you want to replicate this behaviour without the library, check out the source code for this small method.

How do I change the style of edit text in Android Studio?

You can use the attribute style="@style/your_style" that is defined for any widget. The attribute parent="@android:style/Widget. EditText" is important because it will ensure that the style being defined extends the basic Android EditText style, thus only properties different from the default style need to be defined.

What is editable text in Android?

This is the class for text whose content and markup can both be changed. This is the interface for text whose content and markup can be changed (as opposed to immutable text like Strings). If you make a DynamicLayout of an Editable, the layout will be reflowed as the text is changed.


2 Answers

You have this in n2Var

 double n2Var = Double.parseDouble(n2,getText().toString());

instead of

 double n2Var = Double.parseDouble(n2.getText().toString());
like image 150
Inducesmile Avatar answered Oct 03 '22 10:10

Inducesmile


in Kotlin you can use this..

edittext.text.toString().toDouble()
like image 27
Saif Jaradat Avatar answered Oct 03 '22 10:10

Saif Jaradat