•
•
•
•
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
![]() |
•
•
Join Date: Aug 2006
Location: South Africa, Durban
Posts: 100
Reputation:
Rep Power: 3
Solved Threads: 8
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:
where the following imports are required:
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
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
NumberFormat real = NumberFormat.getNumberInstance(); real.setGroupingUsed(false); real.setMaximumFractionDigits(2); JFormattedTextField dataInput = new JFormattedTextField(real);
import java.text.NumberFormat; import javax.swing.JFormattedTextField;
.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
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- define variable (C)
- MS Access validation rule help (MS Access and FileMaker Pro)
- not-a-virusadware (Viruses, Spyware and other Nasties)
- have a try if you are interested in... (C++)
Other Threads in the Java Forum
- Previous Thread: Sending XML Message to Web Service
- Next Thread: Bubble Sort a txt file


Linear Mode