Is there a way to convert a double to string. I tried to do it like this x.toString() and it didnt work as well as implicitly converting it.

Is there a way to convert a double to string. I tried to do it like this x.toString() and it didnt work as well as implicitly converting it.

double is a primative not a Class and you cannot call methods on it like a Class/Object.

There are several ways to convert a double to a String...

double value = 1.0;

// This is ok.
String firstDouble = value + "";

// This is better.
String secondDouble = Double.toString(value);

There are other ways, but these two ways will work fine.

Regards,

Nate

This solution doesn't work with big values, try this:

public static void main(String[] args){
    Double d = new Double("100000000000000000");
    BigDecimal big = new BigDecimal(d);
    System.out.println("Double: " + Double.toString(d));
    System.out.println("BigDecimal: " + big.toString());
}

Results:

Double: 1.0E17
BigDecimal: 100000000000000000

which basically means, it does work. but you need to keep in mind that every type has it's limits.
@Makarek: if one wants to use a double, it's doubtfull the value 100000000000000000 whil show up during the programming or running of the program

this doesnt work fine with values like 21761236.24 where the result is 21761236.2399999983608722686767578125

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.