954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

converting double to string

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.

Dark_Omen
Posting Pro
573 posts since Apr 2004
Reputation Points: 23
Solved Threads: 6
 
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

hooknc
Posting Whiz in Training
219 posts since Aug 2005
Reputation Points: 11
Solved Threads: 8
 

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

Makarek
Newbie Poster
1 post since May 2010
Reputation Points: 10
Solved Threads: 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

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You