1 how to convert a string to an integer Int?
A. There are two methods:
1). Int I = integer. parseint ([String]); or
I = integer. parseint ([String], [int Radix]);
2). Int I = integer. valueof (my_str). intvalue ();
Note: The methods for converting strings to double, float, and long are similar.
2. How do I convert an integer int to a string?
A. There are three methods:
1.) string S = string. valueof (I );
2.) string S = integer. tostring (I );
3.) string S = "" + I;
Note: the conversion of double, float, and long into strings is similar.
Conversion of Java data types to ynniebo [favorites]
Keyword type conversion
Source
This is an example of data type conversion in Java.
Package cn.com. lwkj. ERTs. Register;
Import java. SQL. date;
Public class typechange {
Public typechange (){
}
// Change the string type to the int type
Public static int stringtoint (string intstr)
{
Integer integer;
Integer = integer. valueof (intstr );
Return integer. intvalue ();
}
// Change int type to the string type
Public static string inttostring (INT value)
{
Integer integer = new INTEGER (value );
Return integer. tostring ();
}
// Change the string type to the Float Type
Public static float stringtofloat (string floatstr)
{
Float floatee;
Floatee = float. valueof (floatstr );
Return floatee. floatvalue ();
}
// Change the float type to the string type
Public static string floattostring (float value)
{
Float floatee = new float (value );
Return floatee. tostring ();
}
// Change the string type to the sqldate type
Public static java. SQL. Date stringtodate (string datestr)
{
Return java. SQL. Date. valueof (datestr );
}
// Change the sqldate type to the string type
Public static string datetostring (Java. SQL. Date datee)
{
Return datee. tostring ();
}
Public static void main (string [] ARGs)
{
Java. SQL. Date Day;
Day = typechange. stringtodate ("2003-11-3 ");
String strday = typechange. datetostring (day );
System. Out. println (strday );
}
}
Common data type conversion functions in Java
Although they can all be found in Java API, sort out and make a backup.
String-> byte
Byte static byte parsebyte (string S)
Byte-> string
Byte static string tostring (byte B)
Char-> string
Character static string to string (char C)
String-> short
Short static short parseshort (string S)
Short-> string
Short static string tostring (short S)
String-> integer
Integer static int parseint (string S)
Integer-> string
Integer static string tostring (int I)
String-> long
Long static long parselong (string S)
Long-> string
Long static string tostring (long I)
String-> float
Float static float parsefloat (string S)
Float-> string
Float static string tostring (float F)
String-> double
Double static double parsedouble (string S)
Double-> string
Double static string tostring (double D)