Results 1 to 14 of 14
Thread: [SOLVED] String conversion
- 11-28-2008, 03:58 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 15
- Rep Power
- 0
[SOLVED] String conversion
I am trying to convert a string that is a float value to a float then add 25.00 to it and convert it back to a string. I have tried the following.
String balance = rs.getString(9);
float price = Integer.parseInt(balance);
// val = price + 25;
// String ic = Integer.toString(val)
-
You use Integer to parse a String to an int not a float or double. Since Integer is the wrapper class for int, what class should you use to parse a String to float?
Hint: Look in the API and you will find the class and your method.
Also, you rarely want to use float when you can use the more accurate double type.
- 11-28-2008, 05:44 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
You have made few mistakes there.
Java Code:String str = "34.2"; float val = Float.parseFloat(str); float newVal = val + 25.0F; str = String.valueOf(newVal); System.out.println(str);
-
OK, Eranga has given you the answer: to get a float from a String you need to use the Float class and parse it with a parseFloat method. I again recommend however that you not do this, but instead use Double.parseDouble(...)
- 11-28-2008, 05:51 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Yes, that's true. But there is a reason not to mentioned that in my post. Because our thread starter can confuse on float and double. Just look at his attempt. Seems no idea about wrapper classes, he's a newbie to Java. :)
- 11-28-2008, 07:18 AM #6
-
fishtoprecords is absolutely correct. I didn't see that the OP was dealing with currency here.
- 11-28-2008, 07:37 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
And if I'm remember correctly, fishtoprecords took a long discussion about that in one of his past thread. Sorry if I'm wrong.
- 11-28-2008, 07:44 AM #9
Prior thread:
http://www.java-forums.org/new-java/...money-sin.html
- 11-28-2008, 07:48 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
I think it's better to look at our thread starter. May be even before he has to learn about basis as well.
- 11-28-2008, 08:36 AM #11
Member
- Join Date
- Nov 2008
- Posts
- 12
- Rep Power
- 0
polymorphism
can anyone give me a simple source code that demonstrates polymorphism in java?
anyone plz:o
-
- 11-28-2008, 07:09 PM #13
Member
- Join Date
- Nov 2008
- Posts
- 15
- Rep Power
- 0
Thanks
It works now.
- 11-30-2008, 04:17 AM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Similar Threads
-
String to Integer Conversion in JSP
By vinikz in forum New To JavaReplies: 8Last Post: 11-10-2010, 03:45 PM -
String to Object conversion
By moaxjlou in forum New To JavaReplies: 1Last Post: 10-29-2008, 02:04 AM -
string conversion??
By j2vdk in forum New To JavaReplies: 13Last Post: 09-19-2008, 04:35 PM -
bytearray to string conversion
By mew in forum New To JavaReplies: 1Last Post: 01-28-2008, 11:39 AM -
String to Date conversion
By javaplus in forum New To JavaReplies: 2Last Post: 11-06-2007, 08:16 PM
Bookmarks