File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Converting double to string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Converting double to string" Watch "Converting double to string" New topic
Author

Converting double to string

Annie Hampton
Greenhorn

Joined: Mar 08, 2002
Posts: 2
I have a double that I want to display on a UI component that is expecting a String. As in,
txtBox.setText("I want my double value displayed here");
Why can't I cast it? What do I need to do?
TIA!
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

use String.valueOf()
Junilu
Greg Brouelette
Ranch Hand

Joined: Jan 23, 2002
Posts: 144
An even faster trick is to do this:

If you simply concat an empty string with a double (or int or float) it will automatically make it into a String.


For a good Prime, call:<br />29819592777931214269172453467810429868925511217482600306406141434158089
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6


double d = 0.02;
String temp = "" + d;
System.out.println(" the number is " + temp );

If you do this, you don't really need to use a temp variable. The "" + something form seems a little kludgy but that's probably just me.
Junilu
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Converting double to string