java.math.BigDecimal cannot be cast to java.lang.Integer说类型不能转换,求解释!

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java.math.BigDecimal cannot be cast to java.lang.Integer说类型不能转换,求解释!相关的知识,希望对你有一定的参考价值。

第七十八行报错,debug说list.get(i).get("goods_amout")不能转化为integer类型

将其改为:

classroom.ClassroomBuilding = int.Parse(buildnum)
classroom.ClassroomStep = int.Parse(floor)

隐试转换说明:

1.两个参数至少有一个是 NULL 时,比较的结果也是 NULL,例外是使用 <=> 对两个 NULL 做比较时会返回 1,这两种情况都不需要做类型转换

2.两个参数都是字符串,会按照字符串来比较,不做类型转换

3.两个参数都是整数,按照整数来比较,不做类型转换

4.十六进制的值和非数字做比较时,会被当做二进制串

5.有一个参数是 TIMESTAMP 或 DATETIME,并且另外一个参数是常量,常量会被转换为 timestamp

6.有一个参数是 decimal 类型,如果另外一个参数是 decimal 或者整数,会将整数转换为 decimal 后进行比较,如果另外一个参数是浮点数,则会把 decimal 转换为浮点数进行比较

7.所有其他情况下,两个参数都会被转换为浮点数再进行比较

所以,下面的几个sql语句有相同的效果:

select * from convert_test where areacode=0001 and period>='20170511' and period<='20170511'; 

select * from convert_test where areacode=1 and period>='20170511' and period<='20170511'; 

select * from convert_test where areacode=0001.0 and period>='20170511' and period<='20170511'; 

select * from convert_test where areacode=1.0 and period>='20170511' and period<='20170511';

参考技术A Object不能直接转化成Integer
你首先需要把它转化成String
list.get(i).toString();
再使用
Integer.valueOf(list.get(i).toString());
有很多转化都是使用String做中继的。。。本回答被提问者采纳
参考技术B 曾经在一个视频上看到,说BigInteger类的处理时按照字符串的处理方式来进行的,直接的用强制类型转换时不行的,但Integer类可以接string类型,可以使用一个Integer密名实例来接BigInteger类。 参考技术C Integer和BigDecimal都是封装类,你用(Integer)这种强制转换肯定是不行的,强制转换只能转换包含继承关系的类或基本类型数据。
BigDecimal d = new BigDecimal(1.2);
Integer i= new Integer(d.intValue());
这样转吧。
参考技术D Integer不是基本类型,是int的包装类,无法把包装当做基本类型来用。

比如,char a = 'c'; int b = (Integer)a; 这样也是回报cannot cast错误的。必须是(int)a,用基本类型才能转。这是第一个问题。

第二个问题 你的list.get(i).get("goods_amout")得到的应该是BigDecimal类型的,这个类型,不能用基本类型强转的。举个简单的例子,string a = "c"; int b = (int)a; 可以这样吗?

要转化这个,用intValue(),也就是:
list.get(i).get("goods_amout").intValue() 这样得到的就是个int 的值了。

java.math.BigDecimal 去掉多余的(0)

private java.math.BigDecimal FundsTotal;public java.math.BigDecimal getFundsTotal()
return FundsTotal;

public void setFundsTotal(java.math.BigDecimal fundsTotal)
FundsTotal = fundsTotal;
输入1000的话 显示的是1000.0000 怎么去掉多余的0

用toString()转换成字符串后,再用substring来截取小数点之前的内容。String strNumber=FundsTotal.toString();strNumber=strNumber.substring(0, strNumber.lastIndexOf(".")); 参考技术A 把 public java.math.BigDecimal getFundsTotal()
return FundsTotal;

改为public BigInteger getFundsTotal()
return FundsTotal.toBigInteger();

以上是关于java.math.BigDecimal cannot be cast to java.lang.Integer说类型不能转换,求解释!的主要内容,如果未能解决你的问题,请参考以下文章

java.math.BigDecimal类multiply的使用

java.math.BigDecimal 不能转换为 java.lang.String?

java.math.BigDecimal 去掉多余的(0)

javaJava.math.BigDecimal.subtract()方法实例

java.math.BigDecimal

java.math.BigDecimal