User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 402,509 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,819 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1299 | Replies: 1
Reply
Join Date: Apr 2007
Posts: 32
Reputation: onsir is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
onsir onsir is offline Offline
Light Poster

limit input only two decimal

  #1  
Jan 8th, 2008
hi,
help me, how to make limit input only two decimal, i used jformattedtext.
example : if i input more than two decimal in jformattedtext, then it can't and automatically delete by backspace.

thanks for your help.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2006
Location: South Africa, Durban
Posts: 100
Reputation: PoovenM is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 8
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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 5:59 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC