简体   繁体   中英

JSpinner - Odd stepsize

I got a JSpinner with a step size of 0.01, but it seems, that the getValue() returns odd values.

As an example: its at 0.06 and if I increase that, it sometimes shows 0.06999999999999999 and not 0.07.

This kind of messes up some of my code, as I need to multiply that by 100 (0.069999999999 * 100 would be 6 and not 7.).

Any possible way to avoid that issue other than using Math.round for my multiplication?

spinnerU.setModel(new SpinnerNumberModel(0.0, 0.0, 30.0, 0.01));
spinnerU.addChangeListener(new ChangeListener() {
    @Override
    public void stateChanged(ChangeEvent e) {
        voltage = (double) spinnerU.getValue();
        lbSpannung.setText(String.format(Locale.US, "%.2f", voltage));
        System.out.println("volt: " + voltage + ", 100x: "  + Math.round((voltage * 100)));
        slider.setValue((int) voltage * 100);
    }
});

output: 
volt: 0.08, 100x: 8
volt: 0.09, 100x: 9
volt: 0.09999999999999999, 100x: 10
volt: 0.1, 100x: 10 

The problem is not with the JSpinner.

You are using a double as model. The type double cannot store values with arbitrary precision. In fact it can't store certain values at all.

You can read more floating point in IT here .

If you want to store precise values you need a kind of integer type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM