@Test
public void testString2Long() {
String str = "1234567890";
long lNum = 0L;
int len = str.length();
for(int x =0; x < len; x++) {
long wq = 1L;
wq = (long) Math.pow(10, len - 1 -x);
//或者下面注释的代码都可以~
// for(int y = 0; y < len - 1 - x; y++) {
// wq *= 10;
// }
lNum += (str.charAt(x) - '0') * wq;
}
System.out.println(lNum - Long.parseLong(str));
}
一道面试题 不用long的相关api将数字字符串转换成long
最新推荐文章于 2023-02-08 16:43:58 发布