wood burning stoves
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

Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
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: 6312

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: 6312

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: http://aspose.com/file-tools
subject: Converting double to string
jQuery in Action, 3rd edition