问题
I am using both JLabels and JTextFields, and need to be able to truncate my doubles to two decimal places when they are displayed by the application.
How can I perform this truncation, preferably without switching to JFormattedTextFields?
回答1:
You can use DecimalFormat class. Here is an explanation
回答2:
Prefer the Swing JFormattedTextField instead; see the Java Tutorial.
来源:https://stackoverflow.com/questions/2525492/how-can-i-output-only-a-certain-number-of-digits-after-a-decimal-place-in-a-jtex