Cannot Convert Double to String

Cannot implicity convert type 'double' to 'string'

You're probably just missing a .ToString():

lblDiameter.Text = (double.Parse(radius.Text) * double.Parse(radius.Text)).ToString();

It'd be clearer and you'd avoid parsing the string twice by storing the number in a local variable:

var value = double.Parse(radius.Text);
lblDiameter.Text = (value * value).ToString();

Now, is a diameter really equal to the square of the radius? ;)

Java, inside a method, cannot convert double to string

The Java class Double contains a static method that will do what you're looking for.

Double.toString(doubleValue)

You could fix your problem in a couple of different ways, depending on the functionality you want your GuitarFubar class to have:

public class GuitarMClass {

public static void main(String[] args) {

// OPTION 1
GuitarFubar output2 = new GuitarFubar();
output2.setGuitarLength(12.2);
String mkShft2 = output2.getGuitarLengthAsString();
System.out.println(mkShft2);

// OPTION 2
GuitarFubar bString = new GuitarFubar();
bString.setGuitarLength(12.2); //your example code never sets the value of guitarLength, which is why one of your outputs is '0.0'
System.out.println(Double.toString(bString.getGuitarLength()));

// OPTION 3
GuitarFubar guitarFubar = new GuitarFubar(12.2); // actually use the guitar length when constructing a new instance
System.out.println(Double.toString(guitarFubar.getGuitarLength()));
}

public class GuitarFubar {
private double guitarLength;

// make sure you have a no-args constructor if that's how you are going to instantiate your class
public GuitarFubar(){
}

public GuitarFubar(double guitarLength){
this.guitarLength = guitarLength;
}

// make sure you have a proper 'getter'
public double getGuitarLength(){
return this.guitarLength;
}

// make sure you have a proper 'setter'
public void setGuitarLength(double guitarLength){
this.guitarLength = guitarLength;
}

// alternative to sillyString method if you want this functionality within your GuitarFubar class
// (although not required since a caller of getGuitarLength() can handle String conversion)
public String getGuitarLengthAsString() {
return Double.toString(this.guitarLength);
}

}

}

How to fix Argument 1: cannot convert from 'double' to 'string' ?

You have to use ToString() to convert the double to a string.

  MessageBox.Show(total.ToString());

Cannot convert Double to String

The reason why you can't do that is because String doesn't have an initializer accepting a double or a float, whereas it implements initializers for all integer types (Int, Uint, Int32, etc.).

So @derdida's solution is the right way to do it.

I discourage using the description property. It is not meant to convert a double to a string, but to provide a human readable textual representation of a double. So, whereas today's representation coincides with the conversion:

let width = 94.5
println(width) // Prints "94.5"

tomorrow it can be changed to something different, such as:

ninety four point five

That's still human readable, but it's not exactly a conversion of double to string.

This rule is valid for all types (structs, classes, etc.) implementing the Printable protocol: the description property should be used to describe, not to convert.

Addendum

Besides using string interpolation, we can also use the old c-like string formatting:

let pi = 3.1415
let piString = String(format: "%0.2f", arguments:[pi])

let message = "Pi is " + String(format: "%0.2f", arguments:[pi])
println(message)

Could not convert double to string

Your class definition says getLength() will return a string, however your getLength() function actually returns width, which is a double. You probably meant to convert length to a string before you return it.

You could use the to_string() function in the string library, and change your getLength() to

return to_string(length)

Converting double to string

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

This will convert double to String

Cannot convert type 'double' to 'string' for Excel values

The Text property can be used to retrieve the text contents from a Cell. So, you could try to use the property as follows:

private string getCellValue(Worksheet Sheet, int Row, int Column)
{
string cellValue = Sheet.Cells[Row, Column].Text.ToString();
return cellValue;
}

String cannot be converted to double error in a function

You are trying to convert String into double and storing its value in r which is a type of String.

You should create another variable of double type and can store that value in that like this:

public static double heaviside(double x) {
String r = "NaN";
double d = Double.parseDouble(r);
double result;
if (Double.isNaN(x)) return d;
else if (x < 0.0) return result = 0.0;
else if (x == 0.0) return result = 0.5;
else return result = 1.0;
}

I hope it will help you.
Happy coding..!



Related Topics



Leave a reply



Submit