当前位置: 技术问答>java相关
在JSP中怎样将数字型变量转为字符型,如何将字符型变量转为数字型,请各位老大指教
来源: 互联网 发布时间:2015-02-24
本文导语: | 1. Integer to String int i = 42; String str = Integer.toString(i); or String str = "" + i 2. double to String String str = Double.toString(i); 3. long to String String str = Long.toString(l); 4. float to String String str = Float.toStrin...
|
1. Integer to String
int i = 42;
String str = Integer.toString(i); or String str = "" + i
2. double to String
String str = Double.toString(i);
3. long to String
String str = Long.toString(l);
4. float to String
String str = Float.toString(f);
5. String to integer
str = "25";
int i = Integer.valueOf(str).intValue(); or int i = Integer.parseInt(str);
parseInt(String,int),这是正确的格式,以前用parseInt(String)是按八进制转换,
所以在使用parseInt("08")时会返回0,正确的使用方法是parseInt("08",10)指明用十进制转换。(在javascript中会发生这种情况,java中不会出现)
6. String to double
double d = Double.valueOf(str).doubleValue();
7. String to long
long l = Long.valueOf(str).longValue(); or long l = Long.parseLong(str);
8. String to float
float f = Float.valueOf(str).floatValue();
9. decimal to binary
int i = 42;
String binstr = Integer.toBinaryString(i);
10. decimal to hexadecimal
int i = 42;
String hexstr = Integer.toString(i, 16); or String hexstr = Integer.toHexString(i);
hexadecimal (String) to integer
int i = Integer.valueOf("B8DA3", 16).intValue(); or int i = Integer.parseInt("B8DA3", 16);
11. ASCII code to String
int i = 64;
String aChar = new Character((char)i).toString();
12. integer to ASCII code (byte)
char c = 'A';
int i = (int) c; // i will have the value 65 decimal
13. To extract Ascii codes from a String
String test = "ABCD";
for ( int i = 0; i
int i = 42;
String str = Integer.toString(i); or String str = "" + i
2. double to String
String str = Double.toString(i);
3. long to String
String str = Long.toString(l);
4. float to String
String str = Float.toString(f);
5. String to integer
str = "25";
int i = Integer.valueOf(str).intValue(); or int i = Integer.parseInt(str);
parseInt(String,int),这是正确的格式,以前用parseInt(String)是按八进制转换,
所以在使用parseInt("08")时会返回0,正确的使用方法是parseInt("08",10)指明用十进制转换。(在javascript中会发生这种情况,java中不会出现)
6. String to double
double d = Double.valueOf(str).doubleValue();
7. String to long
long l = Long.valueOf(str).longValue(); or long l = Long.parseLong(str);
8. String to float
float f = Float.valueOf(str).floatValue();
9. decimal to binary
int i = 42;
String binstr = Integer.toBinaryString(i);
10. decimal to hexadecimal
int i = 42;
String hexstr = Integer.toString(i, 16); or String hexstr = Integer.toHexString(i);
hexadecimal (String) to integer
int i = Integer.valueOf("B8DA3", 16).intValue(); or int i = Integer.parseInt("B8DA3", 16);
11. ASCII code to String
int i = 64;
String aChar = new Character((char)i).toString();
12. integer to ASCII code (byte)
char c = 'A';
int i = (int) c; // i will have the value 65 decimal
13. To extract Ascii codes from a String
String test = "ABCD";
for ( int i = 0; i