00Java之数字char和String与int互相转换

  1. 减去字符0的ASCII码值(48)
    这样的方法适用于大数处理。
    代码示例如下:
//the method of char2int
char ch = '1';
int n = ch - '0';
//the method of String2int
String str = "1234";
int total;
total = 0;
for(int i = 0; i < str.length(); i++){
	total *= 10;
	total += str.charAt(i) - '0';
}
  1. Integer.parseInt(String)方法
    这样的方法适用于int范围内数的处理。
    代码示例如下:
//the method of String2int
String str="1234";
int n = Integer.parseInt(str);
  1. String类的valueOf方法族
    valueOf方法族将其他类型数据转换为String类,例如int、double、char[]等等。示例代码如下:
int n = 8373;
String str1 = String.valueOf(n);
System.out.println(str1);

double dn = 8434.3434;
String str2 = String.valueOf(dn);
System.out.println(str2);
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值