如何在 JLabel 和 JTextField 上显示非科学格式的值?

发布于 2024-11-14 15:06:15 字数 880 浏览 4 评论 0原文

我有一个字符串值,可以包含双精度、整数、ascii 或字节值,我将该值放入 JLabel 中。我希望 double 和 long 值采用 4000000000000 的形式,而不是 java JLabel 默认打印样式 4.0E12。现在我知道字符串中的数据类型,但我不知道如何使 JLabel 仅显示双精度值和整数值的非科学形式。

这是我到目前为止所尝试的:

String str = value; // string that holds the value

switch (var) // var that says which data type my str is
{
  case LONG:
  //convert my string from scientific to non-scientific here
  break;
  case DOUBLE:
  //convert my string from scientific to non-scientific here
  break;
  case ASCII:
  //do nothing
  break;
  ...
}

JLabel label = new JLabel();
label.setText(str); //Want this to be in non-scientific form

但这种方法仍然只打印科学形式。

编辑:

我的转换看起来像:

str = new DecimalFormat("#0.###").format(str);

也是的,它是一个长值,为了清楚起见,我省略了一些数据类型变量。 我不知道这是否适用于所有情况,即使我确实让它工作。我需要它来处理整数、长整型、扩展、双精度和浮点数。

I have a string value that could contain a double, integer, ascii or byte value and I put that value into a JLabel. I would like for the double and long values to be in the form of 4000000000000 instead of the java JLabel default printing style of 4.0E12. Now I know what data type is in the string but I don't know how to make the JLabel display only non-scientific forms for the double and integer values.

Here is what I tried so far:

String str = value; // string that holds the value

switch (var) // var that says which data type my str is
{
  case LONG:
  //convert my string from scientific to non-scientific here
  break;
  case DOUBLE:
  //convert my string from scientific to non-scientific here
  break;
  case ASCII:
  //do nothing
  break;
  ...
}

JLabel label = new JLabel();
label.setText(str); //Want this to be in non-scientific form

but this method still only prints the scientific form.

Edit:

My conversion looks like:

str = new DecimalFormat("#0.###").format(str);

also yes it is a long value, i left out some of the data type variables for clearity.
I dont know if this will work for every case though even if i do get it working. I need it to work for integer, long, xtended, double and float.

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

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

发布评论

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

评论(1

烟雨凡馨 2024-11-21 15:06:15

您必须使用不同的 JLabel,因为默认情况下它不执行任何转换

JFrame frame = new JFrame();
JLabel label = new JLabel();
DecimalFormat df = new DecimalFormat("#0.###");
label.setText(df.format(4e12));
frame.add(label);
frame.pack();
frame.setVisible(true);

显示一个窗口,

4000000000000

我只得到以下转换

DecimalFormat df = new DecimalFormat("#0.###");
System.out.println(df.format(400000000));
System.out.println(df.format(4000000000000L));
System.out.println(df.format(4e12f));
System.out.println(df.format(4e12));

打印

400000000
4000000000000
3999999983616   <- due to float rounding error.
4000000000000

You must be using a different JLabel because it doesn't do any conversion by default

JFrame frame = new JFrame();
JLabel label = new JLabel();
DecimalFormat df = new DecimalFormat("#0.###");
label.setText(df.format(4e12));
frame.add(label);
frame.pack();
frame.setVisible(true);

Displays a window with

4000000000000

I just get the following with that conversion

DecimalFormat df = new DecimalFormat("#0.###");
System.out.println(df.format(400000000));
System.out.println(df.format(4000000000000L));
System.out.println(df.format(4e12f));
System.out.println(df.format(4e12));

prints

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