2
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?

PNC
  • 357
  • 1
  • 5
  • 19

1 Answers1

3

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.

David Lavender
  • 8,021
  • 3
  • 35
  • 55
  • I made it maximum 16 but when exceeds the maximum it rounds off again how can I make it so that he wont accept any inputs greater than 16 digits? – PNC Mar 24 '15 at 16:42
  • 1
    ah ok, that's a bit trickier. You can use a `DocumentFilter` for that. See http://stackoverflow.com/questions/12793030/jtextfield-limiting-character-amount-input-and-accepting-numeric-only – David Lavender Mar 24 '15 at 16:52