Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Need a DecimalFormat for a NumberFormatter for a JFormattedTextField

Posted on 2009-07-13
11
Medium Priority
?
342 Views
Last Modified: 2013-12-29
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

0
Comment
Question by:gdkinney_2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24845875
how is what you have not working?
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24845922
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
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24845937
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:gdkinney_2
ID: 24846004
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
 
LVL 92

Expert Comment

by:objects
ID: 24846026
have you checked the value is being read from database correctly?
Are you using double or float?
0
 

Author Comment

by:gdkinney_2
ID: 24846146
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
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 total points
ID: 24846190
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 24846953
It's worth trying to operate with BigDecimal - you might be running out of precision in certain cases, throwing off the format
0
 
LVL 92

Expert Comment

by:objects
ID: 24846961
double precision is fine
0
 

Author Comment

by:gdkinney_2
ID: 24849917
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 24851267
That's good. Bear in mind that a NumberFormat will use zeros instead of the actual digits if there's a precision overflow
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question