1

Here is the code: How do I make it come out as only 2 decimal places? My app crashes when I use String.format("%.2f", maltRequiredString);

public class MainActivity extends AppCompatActivity {

    EditText batchVolEditText;
    EditText ogEditText;
    EditText bheEditText;
    TextView maltRequiredTextView;


    public void calculate(View view) {

        String batchVolString = batchVolEditText.getText().toString();
        String ogString = ogEditText.getText().toString();
        String bheString = bheEditText.getText().toString();

        double batchVolDouble = Double.parseDouble(batchVolString);
        double ogDouble = Double.parseDouble(ogString);
        double bheDouble = Double.parseDouble(bheString);
        double specificGravity = 0.96;

        double maltRequired = (batchVolDouble * ogDouble * specificGravity) / bheDouble;


        String maltRequiredString = Double.toString(maltRequired);

        maltRequiredTextView.setText(maltRequiredString + "kg");


    }
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
gnrcs
  • 89
  • 3
  • 9
  • Have to tried using ‘printf’ – EOxJ May 27 '18 at 04:49
  • Seems to be a duplicate question. Check - https://stackoverflow.com/questions/2808535/round-a-double-to-2-decimal-places – Lucy Isabelle May 27 '18 at 04:54
  • 1
    Possible duplicate of [Round a double to 2 decimal places](https://stackoverflow.com/questions/2808535/round-a-double-to-2-decimal-places) – Khemraj Sharma May 27 '18 at 04:59
  • You're probably getting this error because `maltRequiredString` is a String. Using `maltRequired` would get you your desired effect – shinjw May 27 '18 at 05:00
  • Welcome to Stack Overflow! Have you taken the [tour](https://stackoverflow.com/tour) yet? The [Help Center](https://stackoverflow.com/help) provides good information about: [how to ask a good question](https://stackoverflow.com/help/how-to-ask); [how to present problems caused by your code](https://stackoverflow.com/help/mcve), [what questions you should avoid asking here](https://stackoverflow.com/help/dont-ask) and [how to tell if your question belongs here](https://stackoverflow.com/help/on-topic). Following these rules will increase your chance of getting the right answer to your question. – q-l-p May 27 '18 at 05:07

2 Answers2

4

Remember you can't use

 String.format("%.2f", maltRequiredString);

Because maltRequiredString is string. Correct coding is that you must use float in this function and for that you have to convert your string to float

float f = Float.valueOf(maltRequiredString);  
String test = String.format("%.02f", f);

You can also use this technique for making it 2 decimal

DecimalFormat decimalFormat = new DecimalFormat("#.##");
float twoDigitsF = Float.valueOf(decimalFormat.format(f));
Zoe
  • 27,060
  • 21
  • 118
  • 148
Keyur Nimavat
  • 3,595
  • 3
  • 13
  • 19
0

You can use DecimalFormat to overcome this problem.

//Make a new instance df that has 2 decimal places pattern.
Decimalformat df = new DecimalFormat("#.##");

// converting maltRequired form double to string.
String maltRequiredString  = df.format(maltRequired);

// then set the value of maltRequiredString to the TextView.
maltRequiredTextView.setText(String.valueOf(maltRequiredString));
Hamed
  • 5,867
  • 4
  • 32
  • 56