Search code examples
androidtemperature

Android temperature conversion force close


I have an app which has a section that needs to convert the temperature from Celsius to Fahrenheit, but for some reason on only a couple phone models (like the htc desire and Sony Ericsson X10) it crashes at this part, but I have no idea why. can anyone help?

double cel = x/10;
  finalTFF = 9.0f/5.0f * cel + 32.0;
   DecimalFormat twoDForm = new DecimalFormat("##.0");
  double NfinalTFF = Double.valueOf(twoDForm.format(finalTFF));
  return twoDForm.format(NfinalTFF);

Solution

  • Why so difficult? As I see, you want to convert a double into a string, so:

    public static String celsiusToFahrenheit(double celsius) throws Exception {
     double result = ((celsius * 9) / 5) + 32;
     return String.format("%.2f", result);
    }
    

    That should work :)