Monday, July 11, 2011

Java String to number conversion

This entry is from http://www.devdaily.com/java/edu/qanda/pjqa00008.shtml

Two ways to convert number to String
Method 1: using String.valueOf()
String s = String.valueOf(i);
String s = String.valueOf(f); // float
String s = String.valueOf(d); // double
String s = String.valueOf(l); // long

Method 2: using toString() method of the numeric classes
String s = new Integer(i).toString(); // or Integer.toString(i)
String s = new Float(f).toString();
String s = new Double(d).toString();
String s = new Long(l).toString();

Convert String to number:
int anInt = Integer.parseInt("17");
double aDouble = Double.parseDouble("17");

NOTE: The methods above are not exhaustive. Googling will surely yield a few more ways to do conversion.

No comments:

Post a Comment