Set a float value to JSpinner with 2 decimals

729 Views Asked by At

I need to set a float value of a JSpinner with 2 decimals, but I always have this error:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: illegal value
at java.desktop/javax.swing.SpinnerNumberModel.setValue(SpinnerNumberModel.java:456)
at java.desktop/javax.swing.JSpinner.setValue(JSpinner.java:355)

all Spinners has this model --> SpinnerNumberModel(0.01, 0.01, 10000.00, 0.01);

float a = Float.parseFloat(aSpinner.getValue().toString());
float b = Float.parseFloat(bSpinner.getValue().toString());
float c = b / 100 * 95;
DecimalFormat decimalformat = new DecimalFormat("#.##");
cSpinner.setValue(decimalformat.format(c));

What am I doing wrong?

I tried to give it a float value and it doesn't work, I tried to give it a String value and it doesn't work.

Can someone help me, please?

1

There are 1 best solutions below

0
Andrew Thompson On

NumberFormat.format(double) returns a String, where as setValue when the spinner has a SpinnerNumberModel will be expecting a numeric value (e.g. float or double).

On a wider note, number formats are only intended to be used for output on a GUI (the view) or printing to the console or a file, etc. Internally (in the model etc) the value should be numeric.