badpanda
badpanda

Reputation: 2454

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?

Upvotes: 1

Views: 1576

Answers (2)

rhu
rhu

Reputation: 963

Prefer the Swing JFormattedTextField instead; see the Java Tutorial.

Upvotes: 2

Enrique
Enrique

Reputation: 10127

You can use DecimalFormat class. Here is an explanation

Upvotes: 2

Related Questions