java - 将double转换为字符串
我不确定是我还是什么,但我在将double转换为字符串时遇到问题。
这是我的代码:
double total = 44;
String total2 = Double.toString(total);
我做错了什么,或者我错过了一步。
我试图转换它时收到错误NumberFormatException。
totalCost.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
try {
double priceG = Double.parseDouble(priceGal.getText().toString());
double valG = Double.parseDouble(volGal.toString());
double total = priceG * valG;
String tot = new Double(total).toString();
totalCost.setText(tot);
} catch(Exception e) {
Log.e("text", e.toString());
}
return false;
}
});
我试图在onTouchListener中执行此操作。 我发布更多代码,基本上当用户触摸edittext框时,我希望信息计算填充edittext框。
13个解决方案
422 votes
double total = 44;
String total2 = String.valueOf(total);
这会将double转换为String
Bhavit S. Sengar answered 2019-03-28T16:07:13Z
14 votes
此代码编译并适用于我。 它使用您尝试的调用将double转换为字符串。
public class TestDouble {
public static void main(String[] args) {
double total = 44;
String total2 = Double.toString(total);
System.out.println("Double is " + total2);
}
}
你看到NumberFormatException让我感到困惑。 查看堆栈跟踪。 我猜你有其他代码,你没有在你的示例中显示导致该异常被抛出。
ditkin answered 2019-03-28T16:07:46Z
12 votes
使用Double.toString(),如果数字太小或太大,您将获得如下科学记数:3.4875546345347673E-6。 有几种方法可以更好地控制输出字符串格式。
double num = 0.000074635638;
// use Double.toString()
System.out.println(Double.toString(num));
// result: 7.4635638E-5
// use String.format
System.out.println(String.format ("%f", num));
// result: 0.000075
System.out.println(String.format ("%.9f", num));
// result: 0.000074636
// use DecimalFormat
DecimalFormat decimalFormat = new DecimalFormat("#,##0.000000");
String numberAsString = decimalFormat.format(num);
System.out.println(numberAsString);
// result: 0.000075
使用String.format()将是最方便的方法。
Nicholas Lu answered 2019-03-28T16:08:19Z
6 votes
异常可能来自parseDouble()调用。 检查给予该函数的值是否真的反映了double。
Stephan answered 2019-03-28T16:08:44Z
3 votes
double priceG = Double.parseDouble(priceGal.getText().toString());
double valG = Double.parseDouble(volGal.toString());
double priceG = Double.parseDouble(priceGal.getText().toString());
double valG = Double.parseDouble(volGal.toString());
double priceG = Double.parseDouble(priceGal.getText().toString());
double valG = Double.parseDouble(volGal.toString());
有用。 必须重复。
agon answered 2019-03-28T16:09:11Z
1 votes
double total = 44;
String total2= new Double(total).toString();
这段代码有效
olyanren answered 2019-03-28T16:09:36Z
1 votes
double priceG = Double.parseDouble(priceGal.getText().toString());
double valG = Double.parseDouble(volGal.toString());
其中一个是抛出异常。 您需要添加一些日志记录/打印才能看到volGal和priceGal中的内容 - 这不是您的想法。
Brian Roach answered 2019-03-28T16:10:04Z
1 votes
double.toString()应该工作。 不是变量类型Double,而是变量本身double。
Matt F. answered 2019-03-28T16:10:31Z
0 votes
只需使用以下内容:
doublevalue+"";
这适用于任何数据类型。
例:
Double dd=10.09;
String ss=dd+"";
Rahul Purohit answered 2019-03-28T16:11:02Z
0 votes
使用StringBuilder类,如下所示:
StringBuilder meme = new StringBuilder(" ");
// Convert and append your double variable
meme.append(String.valueOf(doubleVariable));
// Convert string builder to string
jTextField9.setText(meme.toString());
你会得到你想要的输出。
abhishek sheregar answered 2019-03-28T16:11:36Z
0 votes
你什么时候做的
totalCost.setText(tot);
你这样做
totalCost.setText( "" + total );
其中“”+< 变量> 会自动将int转换为字符串
Francisco Azevedo answered 2019-03-28T16:12:18Z
0 votes
完整信息
您可以将String.valueOf()用于float,double,int,boolean等。
double d = 0;
float f = 0;
int i = 0;
short i1 = 0;
char c = 0;
boolean bool = false;
char[] chars = {};
Object obj = new Object();
String.valueOf(d);
String.valueOf(i);
String.valueOf(i1);
String.valueOf(f);
String.valueOf(c);
String.valueOf(chars);
String.valueOf(bool);
String.valueOf(obj);
Khemraj answered 2019-03-28T16:12:51Z
0 votes
科特林
您可以直接对kotlin中的任何数据类型使用.toString
val d : Double = 100.00
val string : String = d.toString()
Khemraj answered 2019-03-28T16:13:20Z