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.

9

17 Answers

double total = 44; String total2 = String.valueOf(total); 

This will convert double to String

5

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.

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.

3

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.

Kotlin

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

val d : Double = 100.00 val string : String = d.toString() 
double total = 44; String total2 = new Double(total).toString(); 
6

This is a very old post, but this may be the easiest way to convert it:

double total = 44; String total2 = "" + total; 
1
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.

4

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

1

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); 

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);

Just use the following:

doublevalue+""; 

This will work for any data type.

Example:

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

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.

How about when you do the

totalCost.setText(tot); 

You just do

totalCost.setText( "" + total ); 

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

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); 

enter image description here

These are various symbols that are supported as part of pattern in DecimalFomat.

This is a very old post, but java double to string conversion can be done in many ways, I will go through them one by one with example code snippets.

1. Using + operator

This is the easiest way to convert double to string in java.

double total = 44; String total2 = total + ""; 

2. Double.toString()

We can use Double class toString method to get the string representation of double in decimal points. Below code snippet shows you how to use it to convert double to string in java.

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

3. String.valueOf()

double total = 44; String total2 = String.valueOf(total); 

4. new Double(double l)

Double constructor with double argument has been deprecated in Java 9, but you should know it.

double total = 44; //deprecated from Java 9, use valueOf for better performance String total2 = new Double(total).toString(); 

5. String.format()

We can use Java String format method to convert double to String in our programs.

double total = 44; String total2 = String.format("%f", total); 

6. DecimalFormat

We can use DecimalFormat class to convert double to String. We can also get string representation with specified decimal places and rounding of half-up.

double total = 44; String total2 = DecimalFormat.getNumberInstance().format(total); //if you don't want formatting total2 = new DecimalFormat("#.0#").format(total); // rounded to 2 decimal places total2 = new DecimalFormat("#.0#").format(44); // rounded to 2 decimal places 

7. StringBuilder, StringBuffer

We can use StringBuilder and StringBuffer append function to convert double to string.

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like