问题
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));
Error I get
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)
回答1:
Check this
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));
来源:https://stackoverflow.com/questions/32174400/cannot-format-given-object-as-a-number-combobox