LifeLongLearner
LifeLongLearner

Reputation: 57

How to pass double value to a textview in Android? (I'm new)

I'm using EditText to store an input as a double, then using CheckBox to modify the value and store it in the variable "output". I want to display the value of "output" in a TextView when the calculate button is clicked.

Code

public class Cooking extends AppCompatActivity {

public void calculate(View view) {
    EditText userInput = (EditText) findViewById(R.id.user_input);
    String numString = userInput.getText().toString();
    double num = new Double(numString).doubleValue();
    CheckBox ozCheckBox = (CheckBox)findViewById(R.id.oz);
    boolean ozInput = ozCheckBox.isChecked();
    CheckBox gCheckBox = (CheckBox)findViewById(R.id.g);
    boolean gInput = gCheckBox.isChecked();
    if(ozInput == true) {
        double output = num*28.3495;
    } else {
        double output = num;
    }
}

Upvotes: 4

Views: 6978

Answers (3)

Asad Haider
Asad Haider

Reputation: 504

double number = 15.62; 
String numberStr = Double.toString(number); 
textview.setText(numberStr);

Upvotes: 0

Matt Clark
Matt Clark

Reputation: 28629

Assuming you have a textView object

TextView textView = (TextView)findViewById( R.id.myTextView );

String has a method valueOf() that is pretty handy...

double d = 8008135;
textView.setText( String.valueOf( d ) );

You could even just prepend an empty String to it...

textView.setText( "" + d );

Both would give an output of

8008135.0

Upvotes: 2

Zeeshan Shabbir
Zeeshan Shabbir

Reputation: 7114

Here is the guide how to do it

double number = -895.25; 
String numberAsString = new Double(number).toString(); 
textview.setText(numberAsString);

Upvotes: 1

Related Questions