如何将double格式化为字符串(只有两位小数)?

时间:2016-08-13 04:08:45

标签: java android string double string-formatting

我正在使用Android Studio为Android创建应用。我有一个代码块,它收集一些字符串,将它们转换为双精度数,将它们放入方程式,然后将答案放入TextViews,只有两个小数位。

double weightDouble = Double.parseDouble(weightTextField.getText().toString());
                double dehydrationDouble = Double.parseDouble(dehydrationTextField.getText().toString());
                double lossesDouble = Double.parseDouble(lossesTextField.getText().toString());
                double factorDouble = Double.parseDouble(factorTextField.getText().toString());

                double fluidStepOne = (((Math.sqrt(Math.sqrt((weightDouble * weightDouble * weightDouble))))) * 70) * factorDouble;
                double fluidStepTwo = fluidStepOne + (weightDouble * dehydrationDouble * 10);
                double fluid24Hours = fluidStepTwo + lossesDouble;
                double fluidHourInt = fluid24Hours / 24;

                fluidResultLabel.setText(String.format(Locale.US, "%.2f", Double.toString(fluid24Hours)));
                fluidPerHourResultLabel.setText(String.format(Locale.US, "%.2f", Double.toString(fluidHourInt)));

但是,当运行此代码块时,应用程序崩溃,Android Studio会说这个 -

 java.util.IllegalFormatConversionException: %f can't format java.lang.String arguments
 at java.util.Formatter.badArgumentType(Formatter.java:1489)
 at java.util.Formatter.transformFromFloat(Formatter.java:2038)
 at java.util.Formatter.transform(Formatter.java:1465)
 at java.util.Formatter.doFormat(Formatter.java:1081)
 at java.util.Formatter.format(Formatter.java:1042)
 at java.util.Formatter.format(Formatter.java:1011)
 at java.lang.String.format(String.java:1554)
 at com.georgeberdovskiy.fluidtherapy.MainActivity$1.onClick(MainActivity.java:82)
 at android.view.View.performClick(View.java:5204)
 at android.view.View$PerformClick.run(View.java:21153)
 at android.os.Handler.handleCallback(Handler.java:739)
 at android.os.Handler.dispatchMessage(Handler.java:95)                           at android.os.Looper.loop(Looper.java:148)                                       at android.app.ActivityThread.main(ActivityThread.java:5444)                
 at java.lang.reflect.Method.invoke(Native Method)                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:746)           

我的问题是 -

  • 如何解决此问题? (我如何将双重格式化为双十进制字符串)
  • 还有什么我可以改变以使我的代码更短,更不容易出错吗?

谢谢! - 乔治

PS - 这个问题重复,因为与我相同的问题的答案都不起作用,而且我的情况也不同。

3 个答案:

答案 0 :(得分:5)

Double.toString 替换为仅有双倍值,如下所示

String.format(Locale.US, "%.2f", fluid24Hours);

或者 从Java 8开始使用格式化程序如下

NumberFormat formatter =    DecimalFormat.getInstance(Locale.US);
formatter.setMaximumFractionDigits(2);
formatter.format(fluid24Hours);

答案 1 :(得分:2)

double d1 = 123456.78;
double d2 = 567890;
Locale fmtLocale = Locale.getDefault(Category.FORMAT);
NumberFormat formatter = NumberFormat.getInstance(fmtLocale);
formatter.setMaximumFractionDigits(2);
formatter.setMinimumFractionDigits(2);
System.out.println(formatter.format(d1));
System.out.println(formatter.format(d2));
System.out.println(fmtLocale.toLanguageTag());

答案 2 :(得分:1)