## JFinal：java.math.BigDecimal cannot be cast to java.lang.Float

andying 发布于 2014/03/05 10:12

@JFinal 你好，想跟你请教个问题：

java.math.BigDecimal cannot be cast to java.lang.Float

Name      Type         Length        Decimals

price       decimal      10                2

qty的int型与price中的Float型相乖报错，应如何解决?,thks.

```public class Gallery extends Model<Gallery> {
public static final  Gallery dao=new Gallery();

}```

```package com.demo.car;

import com.demo.gallery.Gallery;

public class CarItem {
private Gallery  gallery;
private int qty;
private float  totalPrice;

public CarItem(Gallery  gallery,int qty){
super();
this.gallery=gallery;
this.qty=qty;
}

public Gallery getGallery() {
return gallery;
}
public void setGallery(Gallery gallery) {
this.gallery = gallery;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
public float  getTotalPrice() {
this.totalPrice=this.qty*this.gallery.getFloat("price"); // 此处报错
}

public Float getPrice(){
return this.gallery.getFloat("price");
}

}                                                                                        ```

0

#### 引用来自“黄开源中国”的答案

```BigDecimal bdQty=new BigDecimal(this.qty);
BigDecimal bdPrice=this.gallery.getBigDecimal("price");

this.totalPrice= bdQty.multiply(bdPrice);

2
用 BigDecimal 保存值，或者将 BigDecimal 转成 Float 保存起来

0

#### 引用来自“JFinal”的答案

用 BigDecimal 保存值，或者将 BigDecimal 转成 Float 保存起来

```public BigDecimal  getTotalPrice() {
BigDecimal bd=new BigDecimal(this.qty);
this.totalPrice= this.qty  * this.gallery.getBigDecimal("price");