如何在 JTextComponent 中仅输出小数点后的特定位数?
我同时使用 JLabel
和 JTextField
,并且需要能够在应用程序显示双精度数时将其截断到小数点后两位。
如何执行此截断,最好不切换到 JFormattedTextField?
I am using both JLabel
s and JTextField
s, 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 JFormattedTextField
s?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 DecimalFormat 类。 这里是一个解释
You can use DecimalFormat class. Here is an explanation
更喜欢 Swing
JFormattedTextField
;请参阅 Java 教程。Prefer the Swing
JFormattedTextField
instead; see the Java Tutorial.