Reputation: 4590
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.
Upvotes: 242
Views: 1002252
Reputation: 1256
This is a very old post, but this may be the easiest way to convert it:
double total = 44;
String total2 = "" + total;
Upvotes: 3
Reputation: 1107
When you would like to format the decimal and convert it to a String
DecimalFormat
helps much.
Example:
DecimalFormat format = new DecimalFormat("#.####");
format.format(yourDoubleObject);
These are various symbols that are supported as part of pattern in DecimalFomat.
Upvotes: 0
Reputation: 1679
There are three ways to convert double to String.
""+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
Upvotes: 0
Reputation: 58934
You can use .toString
directly on any data type in kotlin, like
val d : Double = 100.00
val string : String = d.toString()
Upvotes: 2
Reputation: 58934
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);
Upvotes: 2
Reputation: 1
How about when you do the
totalCost.setText(tot);
You just do
totalCost.setText( "" + total );
Where the "" + < variable > will convert it to string automaticly
Upvotes: 0
Reputation: 1893
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.
Upvotes: 39
Reputation: 9
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.
Upvotes: -1
Reputation:
double.toString()
should work. Not the variable type Double
, but the variable itself double
.
Upvotes: 2
Reputation: 8924
double total = 44;
String total2 = String.valueOf(total);
This will convert double to String
Upvotes: 500
Reputation: 7044
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.
Upvotes: 14
Reputation: 51
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.
Upvotes: 4
Reputation: 41
Just use the following:
doublevalue+"";
This will work for any data type.
Example:
Double dd=10.09;
String ss=dd+"";
Upvotes: 0
Reputation: 76898
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.
Upvotes: 1
Reputation: 7388
The exception probably comes from the parseDouble() calls. Check that the values given to that function really reflect a double.
Upvotes: 6