Search code examples
javajtextfieldbigdecimal

JFormattedTextField and BigDecimals


private JFormattedTextField floatingText = new JFormattedTextField();

DecimalFormat df = new DecimalFormat();
NumberFormatter dnff = new NumberFormatter(df);
DefaultFormatterFactory factory = new DefaultFormatterFactory(dnff);
floatingText.setFormatterFactory(factory);

It works perfectly when I enter small numbers such as 1.23 but when I enter BigDecimals like 9.99999999 it rounds it off to 10. What can I do so Formatter can accept BigDecimal?


Solution

  • You can change the min and max digits to display through methods on DecimalFormat. So:

    DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(20);
    

    There are other methods on there to play around with too.