3

I am creating an app for Android using Android Studio. I have a block of code that collects some strings, converts them into doubles, puts them into equations, and then puts the answers into TextViews with only two decimal places.

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)));

However, when this block of code is run, the app crashes, and Android Studio says this-

 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)           

My questions are-

  • How do I fix this problem? (how do I propery format a double into a two-decimal string)
  • Is there anything else I can change to make my code shorter and less prone to bugs?

Thanks!- George

PS- This question is not a duplicate because the answers to questions the same as mine didn't work, and my circumstances are different.

George B
  • 918
  • 3
  • 15
  • 24

5 Answers5

5

Replace Double.toString() with only double value like below:

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

Or from Java 8 use formatter like below:

NumberFormat formatter = DecimalFormat.getInstance(Locale.US);
formatter.setMaximumFractionDigits(2);
String formattedNumber = formatter.format(fluid24Hours);
Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
Prasad
  • 3,462
  • 1
  • 23
  • 28
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());
Maddy
  • 4,525
  • 4
  • 33
  • 53
1

Use this code

fluidPerHourResultLabel.setText(new DecimalFormat("##.##").format(fluidHourInt));

Please check the link Show only two digit after decimal

Community
  • 1
  • 1
Shanto George
  • 994
  • 13
  • 26
  • I know that maybe some answers might also have valid answers, but I tried this one first and it worked. Thanks Shanto. – George B Aug 13 '16 at 05:03
1

Down cast Original double value into string suppose say aString. then use

newTrimmedString = aString.substring(0, Math.min(AString.length(), 5)); <---- 5 Represents total no. of character u wish to print/save upto.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Suchandra T
  • 569
  • 4
  • 8
1

From java 8, this is the way: https://docs.oracle.com/javase/8/docs/api/java/text/DecimalFormat.html

Unai Vivi
  • 3,073
  • 3
  • 30
  • 46