如何在 JLabel 和 JTextField 上显示非科学格式的值?
我有一个字符串值,可以包含双精度、整数、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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须使用不同的 JLabel,因为默认情况下它不执行任何转换
显示一个窗口,
我只得到以下转换
打印
You must be using a different JLabel because it doesn't do any conversion by default
Displays a window with
I just get the following with that conversion
prints