Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (3.9k points)

I am not sure it is me or what but I am having a problem converting a double to string.

here is my code:

double total = 44;

String total2 = Double.toString(total);

Am i doing something wrong or am i missing a step here.

I get error NumberFormatException when trying to convert this.

totalCost.setOnTouchListener(new OnTouchListener() {

  public boolean onTouch(View v, MotionEvent event) {

    try {

      double priceG = Double.parseDouble(priceGal.getText().toString());

      double valG = Double.parseDouble(volGal.toString());

      double total = priceG * valG;

      String tot = new Double(total).toString();

      totalCost.setText(tot);

    } catch(Exception e) {

      Log.e("text", e.toString());

    }

    return false;

  }         

});

I am trying to do this in an onTouchListener. Ill post more code, basically when the user touches the edittext box i want the information to calculate a fill the edittext box.

1 Answer

0 votes
by (46k points)

Try:

double total = 44;

String total2 = String.valueOf(total);

This will convert double to String

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Nov 19, 2019 in Java by Anvi (10.2k points)

Browse Categories

...