Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

How can I convert Fahrenheit to Celsius in Stormy Android app?

Currently temperature is being displayed in Fahrenheit. I want to display in Celsius instead. How can I achieve this?

4 Answers

Mine's a little modified for getting location from GPS but you should be able to follow it.

    protected String getForecastUrl(double latitude, double longitude) {
        return "https://api.forecast.io/forecast/" + ApiKey.getApiKey() +
                "/" + latitude + "," + longitude +
                "?units=si";
    }

I changed my getTemperature() & getTemperatureMax() methods to calculate the conversion:

    public int getTemperatureMax() {
        Double celsius = (mTemperatureMax - 32.0) * 0.5555;
        return (int) Math.round(celsius);
    } 

    public int getTemperature() {
        double celsius = (mTemperature - 32.0) * 0.5555;
        return (int)Math.round(celsius);
    }

Do that in each of the classes where the method exists. There's probably a nicer way of doing that, rather than having it in three classes, but I let that slide for now!

I hope that helps.

Steve.

It worked for me. But did you tried appending URL to do so?

I changed my version to show Celsius too.

Let me dig my code up and I'll post right back!

Steve.

You can also have forecast.io return Celsius directly by appending "?units=si" to the request URL (or "?units=auto" to choose units based on location).

I tried appending the URL but it didn't work. Can you tell me the code-snippet please?