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

被刻印的时光 ゝ 提交于 2019-12-11 11:53:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!