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

Pieter-Andries van der Berg
Pieter-Andries van der Berg
7,468 Points

Convert to celsius

Hello guys and girls,

Im following the stormy app.

what would be a good way because the data we are getting is in Fahrenheit i want it to display in degrees celcius

6 Answers

Seth Kroger
Seth Kroger
56,413 Points

IIRC you can add "&units=si" or "&units=auto" to the end of the query to get Celsius/metric or the correct units for your current location respectively.

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,076 Points

I am not familiar with any Android libraries since I haven't actually done any android development. But you can manually convert the temperature pretty straightforwardly using some simple math.

The Formula for Fahrenheit to Celsius: Tc = (Tf - 32) x 5 / 9

Where Tc = the final temperature Where Tf = the temperature in Fahrenheit


Example: 50deg Fahrenheit in Deg Celsius = (50 - 32) x 5 / 9 = 10.00 deg Celcius

Pieter-Andries van der Berg
Pieter-Andries van der Berg
7,468 Points

okay thanks for that naother question the time in the app is in PM how can i convert it to 24H format

Dane Parchment
Dane Parchment
Treehouse Moderator 11,076 Points

Again, I don't know if there are any libraries available that will handle this for you, or if you can customize the data you receive, but to convert a 12 hour clock to a 24 hour clock you need to:

  1. If the time is am, remove the AM, and append a leading 0 if the digit is a single number

    1. Example: 9:30am -> 09:30 (single digit)
    2. Example: 10:45am -> 10:45 (double digit)
  2. If the time is pm, remove the PM, and add 12 to the number

    1. Example: 9:30pm -> 21:30
Pieter-Andries van der Berg
Pieter-Andries van der Berg
7,468 Points

i successfully converted the time with the simple date formatted to "HH:mm"

But my phone time is on 17:08 (5:08PM) but the app says 16:08 (4:08PM)

its not getting the +1 hour of my timezone for some reason. ( living in The Netherlands)

using this : formatter.setTimeZone(TimeZone.getTimeZone(timeZone));

alastair cooper
alastair cooper
30,617 Points

Hi

I used a regular expression string to find any numeric value followed by ?F. Then I parsed the number part and changed it to Centigrade with the formula and replaced 'F' with 'C' I have posted the function for you here. I had system print lines in to test it which are commented out.

hope this helps

    public String switchSummaryFahrenheitForCentigrade(String summary) {
        String tempFound = null;
        //System.out.println(summary);
        Pattern p = Pattern.compile("\\d+\\WF");
        Matcher m = p.matcher(summary);
        while(m.find()) {
            tempFound = m.group();
            tempFound = tempFound.substring(0,tempFound.length()-2);
            //System.out.println(tempFound);
        }
        if(tempFound != null) {
            int newTemp = Math.round((Integer.valueOf(tempFound) - 32) * 5/9);
            //System.out.println(newTemp);
            summary = summary
                    .replace(tempFound,String.valueOf(newTemp))
                    .replace("F ","C ");
        }
        //System.out.println(summary);
        return summary;
    }

NOTE: in a regex tester the actual pattern is \d+\WF but you need to escape the backslashes in java (so use 2 of them)

NOTE: This will only change the last instance of the temperature (it will catch them all but the while loop keeps re-writing the value found and only passes on the last value to the rest of the function) However, so far there has never been more than one mention of a temperature in the daily or hourly summaries. Note also, It will also only catch temperatures if they are followed by °F. I tried it without but it converts all numerical data like inches of rain and snow etc. Again, so far this has not mattered.

Overall this is a solution that has served me well so far but is not perfect. I wish I had read the Dark Sky information properly before I started if the last answer is true and I can just ask for the units in Centigrade! That would be easier and never produce errors

Here's an old solution I did when I did the course some time ago. https://teamtreehouse.com/community/solution-for-converting-the-code-into-celsius