0
private JFormattedTextField getCurrencyJFormattedTextField() {
    NumberFormat format= NumberFormat.getNumberInstance();
    format.setMaximumFractionDigits(4);    
    NumberFormatter formatter= new NumberFormatter(format);
    formatter.setMinimum(0.0);
    formatter.setValueClass(Double.class);
    JFormattedTextField formattedTextField = new JFormattedTextField(formatter);
    return formattedTextField;
}

So, if I explicitly key in "1.23456789"

UI of JFormattedTextField will show "1.2346"

However, when call getValue, the returned value is 1.23456789, not 1.23461

final double price = (Double)jFormattedTextField.getValue();

Is there any way to WYSIWYG? So that the underlying data structure will be consistence with UI display?

Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
  • Most of the settings you have supplied are about formatting the underlying value, not restricting it's data entry. About the only thing I can think of is using a `DocumentFilter` to restrict what the user can actually enter... – MadProgrammer Aug 12 '13 at 02:13
  • You might have better luck using a `JSpinner` as seen [here](http://stackoverflow.com/a/10021773/418556). Though note that floating point numbers are generally of no exact value, as you seem to want. – Andrew Thompson Aug 12 '13 at 02:15

1 Answers1

1

You have to use BigDecimal as your data type. It is specifically created for arbitrary-precision signed decimal numbers.

Eugene Ryzhikov
  • 17,131
  • 3
  • 38
  • 60