如何将 double 格式化为字符串(只有两位小数)?
How do I format a double into a string (with only two decimal places)?
我正在使用 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.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- 这个问题 不是 重复的,因为与我相同的问题的答案无效,和我的情况不一样。
使用此代码
fluidPerHourResultLabel.setText(new DecimalFormat("##.##").format(fluidHourInt));
请检查link
Show only two digit after decimal
将 Double.toString() 替换为仅双精度值,如下所示:
String formattedNumber = String.format(Locale.US, "%.2f", fluid24Hours);
或从 Java 8 使用格式化程序如下:
NumberFormat formatter = DecimalFormat.getInstance(Locale.US);
formatter.setMaximumFractionDigits(2);
String formattedNumber = formatter.format(fluid24Hours);
将原始双精度值向下转换为字符串假设说 aString。
然后使用
newTrimmedString = aString.substring(0, Math.min(AString.length(), 5));
<---- 5 代表总数您希望达到 print/save 的性格。
从java8开始,是这样的:
https://docs.oracle.com/javase/8/docs/api/java/text/DecimalFormat.html
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());
我正在使用 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.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- 这个问题 不是 重复的,因为与我相同的问题的答案无效,和我的情况不一样。
使用此代码
fluidPerHourResultLabel.setText(new DecimalFormat("##.##").format(fluidHourInt));
请检查link Show only two digit after decimal
将 Double.toString() 替换为仅双精度值,如下所示:
String formattedNumber = String.format(Locale.US, "%.2f", fluid24Hours);
或从 Java 8 使用格式化程序如下:
NumberFormat formatter = DecimalFormat.getInstance(Locale.US);
formatter.setMaximumFractionDigits(2);
String formattedNumber = formatter.format(fluid24Hours);
将原始双精度值向下转换为字符串假设说 aString。 然后使用
newTrimmedString = aString.substring(0, Math.min(AString.length(), 5));
<---- 5 代表总数您希望达到 print/save 的性格。
从java8开始,是这样的: https://docs.oracle.com/javase/8/docs/api/java/text/DecimalFormat.html
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());