A friendly place for programming greenhorns!
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
Author
Converting double to string
Annie Hampton
Greenhorn
Joined: Mar 08, 2002
Posts: 2
posted
Mar 08, 2002 14:19:00
0
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
I like...
posted
Mar 08, 2002 14:26:00
0
use String.valueOf()
Junilu
Greg Brouelette
Ranch Hand
Joined: Jan 23, 2002
Posts: 144
posted
Mar 08, 2002 17:17:00
0
An even faster trick is to do this:
double d = 0.02; String temp = "" + d; System.out.println(" the number is " + temp );
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
I like...
posted
Mar 09, 2002 03:02:00
0
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
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter