无法将给定对象格式化为数字组合框 [英] Cannot format given Object as a Number ComboBox

查看:105
本文介绍了无法将给定对象格式化为数字组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将整数设置为双精度?假设我有一个名为day的comboBox,它具有1,2,,3个元素。我想将其设置为两个小数点。如果用户选择1,它将变为1.00。我该怎么办?

How can I set an integer number into double? Let's say I have a comboBox named day, and it has 1,2,,3 elements. I want to set it as two decimal point. If user select 1, it will become 1.00. How can I do that?

public void actionPerformed(ActionEvent e){
                        String a=(String)comboBox.getSelectedItem();
                        //Integer b=(comboBox_1.getSelectedIndex()+1);
                        int day=(Integer)comboBox_2.getSelectedItem();
                        double bo;
                        DecimalFormat df = new DecimalFormat("#.##");      
                        bo= Double.valueOf(df.format(day));

错误我得到

java.lang.IllegalArgumentException: Cannot format given Object as a Number
    at java.text.DecimalFormat.format(DecimalFormat.java:507)
    at java.text.Format.format(Format.java:157)
    at gui.User.<init>(User.java:105)
    at gui.User$1.run(User.java:49)


推荐答案

选中此

public void actionPerformed(ActionEvent e){
                    String a=comboBox.getSelectedItem().toString();
                    //Integer b=(comboBox_1.getSelectedIndex()+1);
                    int day=Integer.ParseInt(comboBox_2.getSelectedItem().toString());
                    double bo;
                    DecimalFormat df = new DecimalFormat("#.##");      
                    bo=  Double.parseDouble(df.format(day));

这篇关于无法将给定对象格式化为数字组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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