0

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.

6
Contributors
5
Replies
36
Views
11 Years
Discussion Span
Last Post by JamesCherrill
0

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

1

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

Edited by mike_2000_17: Fixed formatting

0

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

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.