File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Converting double to string

Annie Hampton

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?
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6529

use String.valueOf()
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

Joined: Feb 26, 2001
Posts: 6529

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.
I agree. Here's the link:
subject: Converting double to string
It's not a secret anymore!