Search code examples
javaswingjlabeljtextfieldjtextcomponent

How can I output only a certain number of digits after a decimal place in a JTextComponent?


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?


Solution

  • You can use DecimalFormat class. Here is an explanation