Need a DecimalFormat for a NumberFormatter for a JFormattedTextField

Below is my code.  My JFormattedTextField is numberField.  I need a format (pattern) for the following:

100.00000         needs to appear as    100.000
10000.123456   needs to appear as    10000.123
1.055E9             needs to appear as    1055000000.000
211.E9               needs to appear as    211000000000.000

is there a pattern to do this?  

Thank You
NumberFormatter doubleFormatter = new NumberFormatter(new DecimalFormat("#0.000#"));
doubleFormatter.setAllowsInvalid(false);
numberField.setFormatterFactory(new DefaultFormatterFactory(doubleFormatter));
numberField.setColumns(10);
numberField.setValue(0.0);

Open in new window

gdkinney_2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
how is what you have not working?
0
Kevin CrossChief Technology OfficerCommented:
I think you want this format "#0.000" which will have exactly 3 digits after zero always even if only have precision to tenths or hundreds place.  Additionally, will always have a digit in ones place even if value is less than one.
NumberFormatter doubleFormatter = new NumberFormatter(new DecimalFormat("#0.000"));
doubleFormatter.setAllowsInvalid(false);
numberField.setFormatterFactory(new DefaultFormatterFactory(doubleFormatter));
numberField.setColumns(10);
numberField.setValue(0.0);

Open in new window

0
Kevin CrossChief Technology OfficerCommented:
So your one error is the extra # at the end as it would be a fourth digit after decimal and if you remove one of the zeros and have # instead then numbers with less than three digits would only go out two places.  Therefore, you have to remember that 0 is used in pattern to represent a digit that will be included no matter what either as value or zero.  # are only included when have non-zero value.
0
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

gdkinney_2Author Commented:
The one problem with what I have is that if I have a large number like

2.23E19  in my database when the value populates my JFormattedTextField it appears as 0 for some reason.  It does not do this for smaller numbers though.
0
Mick BarryJava DeveloperCommented:
have you checked the value is being read from database correctly?
Are you using double or float?
0
gdkinney_2Author Commented:
The value is correct when it comes out of the database it goes to 0 when I attempt to use the correct value to set the text for the JFormattedTextField.  It's correct before I set the JFormattedTextField but then the textfield displays a 0 after I use the correct number and attempt to set it.  If I use a smaller number like 1.0E5 it works OK for some reason.
0
Kevin CrossChief Technology OfficerCommented:
This works fine, so maybe try to parse the data from database into a double variable first and see if makes a difference.
JFormattedTextField numberField = new JFormattedTextField();
		NumberFormatter doubleFormatter = new NumberFormatter(new DecimalFormat("#0.000"));
		doubleFormatter.setAllowsInvalid(false);
		numberField.setFormatterFactory(new DefaultFormatterFactory(doubleFormatter));
		numberField.setColumns(10);
		numberField.setValue(2.23E19);
		JOptionPane.showMessageDialog(null, numberField.getText());

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
It's worth trying to operate with BigDecimal - you might be running out of precision in certain cases, throwing off the format
0
Mick BarryJava DeveloperCommented:
double precision is fine
0
gdkinney_2Author Commented:
Actually I did get it to work I was using numberField.setText then I switched over to numberField.setValue apparently the first way was not using the Formatter correctly.
0
CEHJCommented:
That's good. Bear in mind that a NumberFormat will use zeros instead of the actual digits if there's a precision overflow
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.