【发布时间】:2011-06-08 14:51:27
【问题描述】:
我有一个可以包含双精度、整数、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);
也是的,它是一个长值,为了清楚起见,我省略了一些数据类型变量。 我不知道这是否适用于所有情况,即使我确实让它工作。我需要它来处理整数、长整数、扩展、双精度和浮点数。
【问题讨论】:
-
听起来您的转换不正确。您能否提供如何进行转换的代码? JLabel 只会打印您给它的文本。如果文本是错误的,你给它的文本是错误的。顺便说一句,
int的值不可能那么大,你的意思是long吗? -
@Peter Lawrey:我已经提供了附加代码以及 int to long 解释
标签: java jlabel scientific-notation