java中 将数字转换成字符 java中怎么将数字转换成字符串

java\u4e2d\u600e\u6837\u5c06\u6574\u578b\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u578b\uff1f

public class T6 {

public static void main(String[] args) {
int a = 6;
System.out.println(a+"");
System.out.println(String.valueOf(a));
System.out.println(((Integer)a).toString());
}

}

\u4e09\u79cd\u65b9\u6cd5\uff0c\u4f60\u90fd\u53ef\u4ee5\u8bd5\u8bd5

toString()\u65b9\u6cd5\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u3002
JAVA\u7684\u6574\u578b\u4e0e\u5b57\u7b26\u4e32\u76f8\u4e92\u8f6c\u6362
1\u3001\u5c06\u5b57\u4e32 String \u8f6c\u6362\u6210\u6574\u6570 int
1). int i = Integer.parseInt([String]); \u6216
i = Integer.parseInt([String],[int radix]);
2). int i = Integer.valueOf(my_str).intValue();
\u6ce8: \u5b57\u4e32\u8f6c\u6210 Double, Float, Long \u7684\u65b9\u6cd5\u5927\u540c\u5c0f\u5f02.
2\u3001\u5c06\u6574\u6570 int \u8f6c\u6362\u6210\u5b57\u4e32 String
1.) String s = String.valueOf(i);
2.) String s = Integer.toString(i);
3.) String s = "" + i;
\u6ce8: Double, Float, Long \u8f6c\u6210\u5b57\u4e32\u7684\u65b9\u6cd5\u5927\u540c\u5c0f\u5f02.
Java\u6570\u636e\u7c7b\u578b\u8f6c\u6362 ynniebo \uff1a\u8fd9\u662f\u4e00\u4e2a\u4f8b\u5b50,\u8bf4\u7684\u662fJAVA\u4e2d\u6570\u636e\u6570\u578b\u7684\u8f6c\u6362.\u4f9b\u5927\u5bb6\u5b66\u4e60\u5f15
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);
}
}
JAVA\u4e2d\u5e38\u7528\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u51fd\u6570
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)

方法一:直接强制转换。如:String str= (String)123;
方法二:直接通过空字符串+数字的形式转换为字符串(前后都可以用)。如:String str= ""+123;
方法三:直接通过包装类来实现。如:String str = String.valueOf(1231);

//import java.util.Scanner;
public class welcome {
public static void main(String[] args) {
// Scanner input=new Scanner(System.in);
int count = 0;
for (int i = 33; i <= 126; i++) {
char c = (char) i;//i强制转换成char类型之后为什么不使用?
{
// System.out.print(i + " ");//写错了 是c不是i,i是int类型的当然直接就输出数字了
System.out.print(c + " ");
count++;
if (count % 10 == 0)
System.out.println();
}
}
}
}


i+" "会把‘i’转换成字符串的,而“++”只适用于数字类型的,所以会报错

public class welcome {
public static void main(String[] args){
// Scanner input=new Scanner(System.in);
int count=1;
for(int i=33;i<=126;i++)
{
char c=(char)i;
System.out.print(c+"\t");
count++;
if(count%10==0)
System.out.println();
}
}
}

public static void main(String[] args){
// Scanner input=new Scanner(System.in);
int count=0;
for(int i=33;i<=126;i++)
{
char c=(char)i;
{
System.out.print(c+" ");
count++;
if(count%10==0)
System.out.println();
}
}
}

扩展阅读:java ascii码转字符 ... java判断输入为数字 ... java 字符串转数字 ... java中字符char转数字 ... java怎样输入一个字符 ... java字符串转数字 ... java字符型转换成整型 ... java数字变字母 ... java如何将字符转化为数字 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网