无法将Double数字放入BigDecimal变量中 [英] Can't put Double number in BigDecimal variable

查看:285
本文介绍了无法将Double数字放入BigDecimal变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个Double变量来保存商品价格.该变量存储在postgresql数据库中的money类型列下.我使用setBigDecimal(position,value)SQL函数.另一方面,我使用JSpinner作为输入.

I'm using a Double variable which holds the Item price. That variable is stored in postgresql database under a column of money type. I use setBigDecimal(position,value) SQL function.In other part, i'm using a JSpinner as input.

Double current = 0.0;
Double min = (double) Integer.MIN_VALUE;
Double max = (double) Integer.MAX_VALUE;
Double step = 0.1;

JSpinner priceSpinner = new JSpinner(new SpinnerNumberModel(current, min, max, step));

当用户单击按钮时,我得到了用户输入的值,并通过SQL查询将其放入数据库中.

When the user clicks on a button, I get the value entred by the user and put it in the database via SQL query.

 insertStmt.setBigDecimal(position,BigDecimal.valueOf((double) priceSpinner.getValue()));

但是,我遇到了这个小错误,

But, i got this little error,

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Double

推荐答案

该程序说明Double和BigDecimal之间的双向转换:

This program illustrates conversion in both directions between Double and BigDecimal:

import java.math.BigDecimal;

public class Test {
  public static void main(String[] args) {
    Double d1 = 1.3;
    BigDecimal bd1 = BigDecimal.valueOf(d1.doubleValue());
    Double d2 = bd1.doubleValue();
    System.out.println(d2);
  }
}

请注意,转换为Double可能不准确.

Note that the conversion to Double may not be exact.

这篇关于无法将Double数字放入BigDecimal变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆