如何在 JTextComponent 中仅输出小数点后的特定位数?

发布于 2024-08-27 10:02:22 字数 133 浏览 8 评论 0原文

我同时使用 JLabelJTextField,并且需要能够在应用程序显示双精度数时将其截断到小数点后两位。

如何执行此截断,最好不切换到 JFormattedTextField?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

太阳公公是暖光 2024-09-03 10:02:22

您可以使用 DecimalFormat 类。 这里是一个解释

You can use DecimalFormat class. Here is an explanation

二手情话 2024-09-03 10:02:22

更喜欢 Swing JFormattedTextField ;请参阅 Java 教程

Prefer the Swing JFormattedTextField instead; see the Java Tutorial.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文