Reference http://how2j.cn/k/number-string/number-string-parse/317.html
Numeric to String
Method 1: Use the static method of the String class valueof
Method 2: Boxing the base type as an object and then calling the object's ToString
package digit; Public class Testnumber { publicstaticvoid main (string[] args) { int 5; // Method 1 String str = string.valueof (i); // Method 2 Integer it = i; = it.tostring (); }}
String to Number
static method for calling integer parseint
package digit; Public class Testnumber { publicstaticvoid main (string[] args) { " 999"; int i= integer.parseint (str); System. out . println (i); }}
Practice-String Conversions
Refer to the steps above
Convert floating point 3.14 to string "3.14"
Then convert the string "3.14" to a floating-point number 3.14
If the string is 3.1a4, what will be converted to a floating-point number?
Public Static voidMain (string[] args) {floatf=3.14f; String Str=string.valueof (f); System. out. println (str); F=float.parsefloat (str); System. out. println (f); F=float.parsefloat ("3.1a4");//Run timeexception Java.lang.NumberFormatException:For Input string: "3.1a4"System. out. println (f); }
Results
string conversion of Java