[java] Converting double to string

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.

This question is related to java android

The answer is


double.toString() should work. Not the variable type Double, but the variable itself double.


double total = 44;
String total2= new Double(total).toString();

this code works


This code compiles and works for me. It converts a double to a string using the calls you tried.

public class TestDouble {

    public static void main(String[] args) {
        double total = 44;
        String total2 = Double.toString(total);

        System.out.println("Double is " + total2);
    }
}

I am puzzled by your seeing the NumberFormatException. Look at the stack trace. I'm guessing you have other code that you are not showing in your example that is causing that exception to be thrown.


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

One of those is throwing the exception. You need to add some logging/printing to see what's in volGal and priceGal - it's not what you think.


There are three ways to convert double to String.

  1. Double.toString(d)
  2. String.valueOf(d)
  3. ""+d

    public class DoubleToString {

    public static void main(String[] args) {
        double d = 122;
        System.out.println(Double.toString(d));
        System.out.println(String.valueOf(d));
        System.out.println(""+d);
    
    }
    

    }

String to double

  1. Double.parseDouble(str);

How about when you do the

totalCost.setText(tot);

You just do

totalCost.setText( "" + total );

Where the "" + < variable > will convert it to string automaticly


Just use the following:

doublevalue+""; 

This will work for any data type.

Example:

Double dd=10.09;
String ss=dd+"";

The exception probably comes from the parseDouble() calls. Check that the values given to that function really reflect a double.


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

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

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

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

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

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

it works. got to be repetitive.


Use StringBuilder class, like so:

StringBuilder meme = new StringBuilder(" ");

// Convert and append your double variable
meme.append(String.valueOf(doubleVariable));

// Convert string builder to string
jTextField9.setText(meme.toString());

You will get you desired output.


Kotlin

You can use .toString directly on any data type in kotlin, like

val d : Double = 100.00
val string : String = d.toString()

Complete Info

You can use String.valueOf() for float, double, int, boolean etc.

double d = 0;
float f = 0;
int i = 0;
short i1 = 0;
char c = 0;
boolean bool = false;
char[] chars = {};
Object obj = new Object();


String.valueOf(d);
String.valueOf(i);
String.valueOf(i1);
String.valueOf(f);
String.valueOf(c);
String.valueOf(chars);
String.valueOf(bool);
String.valueOf(obj);

Using Double.toString(), if the number is too small or too large, you will get a scientific notation like this: 3.4875546345347673E-6. There are several ways to have more control of output string format.

double num = 0.000074635638;
// use Double.toString()
System.out.println(Double.toString(num));
// result: 7.4635638E-5

// use String.format
System.out.println(String.format ("%f", num));
// result: 0.000075
System.out.println(String.format ("%.9f", num));
// result: 0.000074636

// use DecimalFormat
DecimalFormat decimalFormat = new DecimalFormat("#,##0.000000");
String numberAsString = decimalFormat.format(num);
System.out.println(numberAsString);
// result: 0.000075

Use String.format() will be the best convenient way.