I am currently trying to create a JSpinner that only accepts monetary values, i.e. a maximum of 2 decimals, and I tried to accomplish this using this code:
priceSpinner = new JSpinner();
SpinnerNumberModel priceSpinnerModel = new SpinnerNumberModel(
Double.valueOf(0d),
Double.valueOf(0d),
null,
Double.valueOf(0.01d));
priceSpinner.setModel(priceSpinnerModel);
priceSpinner.setEditor(new JSpinner.NumberEditor(priceSpinner, "0.00"));
However, when I input a number that contains more than 2 decimals, the spinner rounds it off to 3 decimals instead. What am I doing wrong?
Works fine for me. I cannot reproduce your problem. Consider the below code.
If I enter a number with more than two digits after the decimal point, it gets rounded to precisely two digits.
Refer to How to Use Spinners and javadoc for class java.text.DecimalFormat