无法将给定对象格式化为数字组合框
Posted
技术标签:
【中文标题】无法将给定对象格式化为数字组合框【英文标题】:Cannot format given Object as a Number ComboBox 【发布时间】:2015-08-24 04:22:28 【问题描述】:如何将整数设置为双精度数?假设我有一个名为 day 的组合框,它有 1,2,,3 个元素。我想将其设置为两位小数。如果用户选择 1,它将变为 1.00。我该怎么做?
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)
【问题讨论】:
没有足够的代码来重现您的问题。DecimalFormat.format(long)
(您在代码 sn-p 中调用)应该适用于所有 int 值。您确定例外来自这一行吗?
先用DecimalFormat df = new DecimalFormat("#.00");
再df.format(day)
,不用双重转换
int i = 3;双 d = (双) i;
是否可以像我一样将 int comboBox 格式化为两位小数?
@anirudh 组合框怎么样? int day=(Integer)comboBox_2.getSelectedItem();
【参考方案1】:
检查一下
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));
【讨论】:
bo 不能是 double 类型...我希望它是 double 类型 ya..将bo的类型更改为String..无法从字符串转换为双精度 转换为双精度的字符串在哪里? 字符串类型的 doubleValue() 方法未定义 好的,那么这将帮助你.. Double.parseDouble(df.format(day));以上是关于无法将给定对象格式化为数字组合框的主要内容,如果未能解决你的问题,请参考以下文章