0

So, I have this following code to make a simple temperature app converter in Android Studio 4.0.1:

bt_converter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    double tempC = Double.parseDouble(et_tempC.getText().toString());

    DecimalFormat arredondar = new DecimalFormat("#.##");
    double tempF = Double.parseDouble(arredondar.format(tempC * 1.8 + 32));

    tv_tempF.setText(String.valueOf(tempF));
}

The problem is that when I run the app it crashes due to this statement: DecimalFormat arredondar = new DecimalFormat("#.##"); And the same for: DecimalFormat arredondar = new DecimalFormat("0.00");

In fact it crashes with every pattern started by "0" and involving point "."

I've tried to switch it to: DecimalFormat arredondar = new DecimalFormat("#,##");

And it actually works but it is not showing two decimal places as intended, and no matter what tempC value I put in, the output is always some number point zero (xx.0).

For example: for tempC = 10.11111111 it shows tempF = 50.0 When it should give me 50.19 or 50.2 instead.

I've reinstalled the app and cleaned the project, and even changed my windows 10 region settings (with regards to decimal symbols), but it still remains the same.

But please let me know if you need more details.

Red
  • 26,798
  • 7
  • 36
  • 58
  • does [this](https://stackoverflow.com/questions/9366280/android-round-to-2-decimal-places) help? – Blackbelt Nov 13 '20 at 08:23
  • Please: *ALWAYS* copy/paste the error message text whenever you post a question. SUGGESTION: Try this format, post back what happens: `"#,#00.0#"` – paulsm4 Nov 13 '20 at 18:11
  • Hi, Ann Zen! I'm thankful for your answer. Unfortunately it didn't work yet because no matter what value I put in for tempC, the app crashes imediately. Maybe something is missing or either wrong in the code because it's not accepting neither "0" or "." in the DecimalFormat pattern. But I'm still open to any other suggestions and I appreciate very much your help! – Mario Furtado Nov 14 '20 at 15:08

1 Answers1

0

So, I came to a solution, and it works as intended. I just changed the three last lines of code:

Before

DecimalFormat arredondar = new DecimalFormat("#.##");

double tempF = Double.parseDouble(arredondar.format(tempC * 1.8 + 32));
tv_tempF.setText(String.valueOf(tempF));

After:

DecimalFormat arredondar = new DecimalFormat("0.00");
double tempF = tempC * 1.8 + 32;

tv_tempF.setText(arredondar.format(tempF));

Apparently there is some king of error/incompatibility. But, I realised that I was converting tempF from String to Double with the second line in the before code, and then convert it back again to String with the third one.

So I simply put the tempF directly in the output with a String format, after having calculated it with no convertion:

            double tempF = tempC * 1.8 + 32;

            tv_tempF.setText(arredondar.format(tempF));

And I've changed the pattern from "#.##" to "0.00" to make sure that I get two decimal places everytime.

Dharman
  • 30,962
  • 25
  • 85
  • 135