Java 将双精度转换为带2个小数的字符串

Java 将双精度转换为带2个小数的字符串,java,android,Java,Android,我的健身申请遇到了棘手的问题。 我找到了很多方法,如何将我的距离(双精度)输出转换为小数点后两位的字符串。问题来了-当我想以“%d”格式将其打印到textview(因为它与整数相乘)时,应用程序会遇到IllegalFormatConversionException 卡路里的作用很好 有人能告诉我我做错了什么吗 private int numSteps; private double distance; private double total_energy; private double cal

我的健身申请遇到了棘手的问题。 我找到了很多方法,如何将我的距离(双精度)输出转换为小数点后两位的字符串。问题来了-当我想以“%d”格式将其打印到textview(因为它与整数相乘)时,应用程序会遇到IllegalFormatConversionException 卡路里的作用很好

有人能告诉我我做错了什么吗

private int numSteps;
private double distance;
private double total_energy;
private double calories;
private int weight=80;

//Steps output
            count_steps.setText(String.valueOf(numSteps));
            numSteps++;

            //Distance output
            count_distance.setText(String.format("%d", distance + "m"));
            distance= numSteps*0.8;

            //Calories output
            count_calories.setText(String.format("%.2f", calories));
            total_energy=(0.985*SystemClock.elapsedRealtime()*weight);
            calories=total_energy/4.2;

您可以使用
DecimalFormat
double
格式化为
String
,例如:

DecimalFormat format = new DecimalFormat("#.##");
double d = 3232.432432;
System.out.println(format.format(d));

您可以使用
DecimalFormat
double
格式化为
String
,例如:

DecimalFormat format = new DecimalFormat("#.##");
double d = 3232.432432;
System.out.println(format.format(d));

它仍然给我错误“IllegalArgumentException:Bad class:class java.lang.String”你能试着声明
“#.###”
为常量然后使用它吗?同样的错误但更长的异常输出编辑:constantI的输出仍然没有得到它,很抱歉它仍然给我错误“IllegalArgumentException:Bad class:java.lang.String”你能试着将
“#.##”
声明为常量,然后使用它吗?相同的错误,但更长的异常输出编辑:constantI的输出仍然无法获取抱歉