RSS Forums RSS
Please support our Java advertiser: Lunarpages Java Web Hosting

limit input only two decimal

Join Date: Aug 2006
Location: South Africa, Durban
Posts: 129
Reputation: PoovenM is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 11
PoovenM PoovenM is offline Offline
Junior Poster

Solution Re: limit input only two decimal

  #2  
Jan 8th, 2008
I'm a bit confused as to what you're saying But I do understand that you want to only allow real input with two decimal places. If you have a GUI then JFormattedTextField is the class you're looking for and this is how you'd use it:
NumberFormat real = NumberFormat.getNumberInstance();
real.setGroupingUsed(false);
real.setMaximumFractionDigits(2);
JFormattedTextField dataInput = new JFormattedTextField(real);
where the following imports are required:
import java.text.NumberFormat;
import javax.swing.JFormattedTextField;
What actually happens is that if you type in a number with too many decimal places, you'd see that the text field would automatically remove them. That's 'cause we .setMaximumFractionDigits(2). We can also automatically enforce that two decimal places must be met by using real.setMinimumFractionDigits(2); and even if the user types 1, the input will change to 1.00 (after the user has changed focus to another GUI component).

I haven't actually tried to do that via a console. I would imagine that unless you read in the value, you wouldn't really know how many decimal places are there... but of course where there is Java there is a way
Reply With Quote  
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 9:35 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC