aspose file tools
The moose likes Beginning Java and the fly likes Converting double to string Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply 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: 4118
    
    2

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: 4118
    
    2


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://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Converting double to string
 
Similar Threads
Conversion of a string into Double
how to avoid -e exponential representation in servlet
String conversion in to double
escape character nightmare - please help
String concatenation