Salesforce: System.TypeException: Invalid integer: 2185340704

震惊!明明是一个数字,为什么却得到了这样一个报错信息:

System.TypeException: Invalid integer: 2185340704

类似代码如下:

String test = '2185340704';

Integer testIInt = Integer.valueOf(test);

2185340704 今天就算是天王老子来了它也是一个数字!

悲愤交加,忧思难耐!却在翻阅了一些资料之后豁然开朗,柳暗花明又一蠢~

原来Integer.valueOf最多只能处理9位数的数字,超过9位就会得到这个错误。

那么问题来了,如果我要处理超过9位的数字该咋办呢?

使用Long即可,Long最多支持19位的数字~ Wow~

Integer testInt = Long.valueOf(test);

另外一点要注意的是,一定要注意存储了数字的String是否还包含了空格,如果不处理掉空格的话,一样会报错

例如:

String test = '218 ';

Integer testIInt = Integer.valueOf(test);

这个时候我可以通过添加trim()来避免这一问题

String test = '218 ';

Integer testIInt = Integer.valueOf(test.trim());

 

posted @ 2021-12-17 15:10  Clsriz  阅读(158)  评论(1编辑  收藏  举报