-3

just curious how will I define the following in forms.

c#

 weather.Temperature = (string)results["main"]["temp"]  ;

xaml

<Label x:Name="tempLabel"
            Text="{Binding Temperature , StringFormat='{0}°'F}"/> 

my current result : 270.50 it should be 83 F

Any ideas? Thanks

Pxaml
  • 651
  • 2
  • 12
  • 38

3 Answers3

1

First, you need to convert the temperature manually, from Celsius to Fahrenheit vice-versa whatever you want to (Please look into this over the net, the formula it is). By conversion from decimal to whole number, you can use Math.Round(yourNumber, 0);. Then bind the result to your XAML.

EDIT: You can also do this by using a converter {Binding value, Converter={StaticResource YourConverter} so that the code would be reusable

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    var item = value.ToString();
    item = //your fahrehnheit converter formula 
          //do necessary conversions string to double etc.
    return item;        
}
jbtamares
  • 778
  • 7
  • 26
1

Disragarding the conversion °F<->°C you can use the format string to display whole numbers only

<Label x:Name="tempLabel"
    Text="{Binding Temperature , StringFormat='{0:F0}°F'}"/> 

(please note, that I moved the 'F' within the quotation marks in order to be displayed correctly). The F0 format string is a fixed decimals floating point formatter with zero decimals. F1 would have one decimal number, F2 two and so on. If you'd like to read more about the format strings for numbers, please see here.

To convert the °C value to °F you could for example use an IValueConverter (see here), or convert the value directly in your viewmodel.

EDIT

I missed the point that Temperature was a string. In order to use the number formatting, Temperature has to be a number. Convert it the following way

weather.Temperature = double.Parse((string)results["main"]["temp"])

(of course you'll have to change the type of the Temperature property to double?);

Paul Kertscher
  • 9,416
  • 5
  • 32
  • 57
0

I found out the service let me define the units , all I need to do is add &units=imperial as a parameter to the service call and it will return the temperature in Fahrenheit. Also , I applied @Paul Kertscher edit to display only the whole number. Thank you all for your answers.

Pxaml
  • 651
  • 2
  • 12
  • 38