Reputation: 6794
I am trying to figure out how to do two things :
String
to a double
and back to a String
.double
.To explain :
In the first case, I would like the user to be able to input a number into the a JTextArea
so I can store the input in a variable (simple). Then, I would like to perform operations on the number that is inputted, but this cannot be done with a string. Is there any way to convert input into a double
?
Then, I would like to take this string, after it has been converted into a double, and analyze the specific digits, and the digits of its decimal places (tenths, hundredths, etc.) as well using if-else and for statements (I can do this part). My understanding of strings is that, if I say NameofString[0]
, I will get the first letter of the string, but can this be done with a Number
?
Lastly, I would like to convert this double
back into a String
.
Upvotes: 0
Views: 832
Reputation: 12883
Also, NameofString[0]
will not work in Java. but you can call myString.charAt(0)
though. To your question about extracting digits from a double would either require conversion to a string or doing some math on the number to pull digits out.
Upvotes: 1
Reputation: 12102
The translation part is discussed in the answer by @Howard, Now for analyzing specific digits, I suppose you need to use the /
and %
judiciously.
say for example,
while(x!>0)
{
x%10 // will return the digit at the tenths position.
Now,
x=x/10 //will give you the remaining part.
}
Upvotes: 1
Reputation: 39197
You can parse a string using (assuming x
is a double variable and s
a string)
x = Double.parseDouble(s);
and convert the number back to a string via
s = Double.toString(x);
If you want to format the value according to a specific format you may want to use String.format
instead.
Upvotes: 5