Integer to string formatted as currency

Since it has been a while since the last post, I thought I would give a quick trick that shows the power of string.Format().

Say you got an integer “price” that you want to display in a string, but formatted as currency. You can do this very easily using string.Format():

int price = 2795000;
string priceStr = string.Format("The price is {0:C}", price);

 

priceStr would now be “The price is 2.795.000,00 kr” if the Culture was set to Swedish. If you want to “lock” the culture do like this:

int price = 2795000;
string priceStr = string.Format(new CultureInfo("en-US"), "The price is {0:C}", price);

 

That would format it as $ instead, giving priceStr the value “$2,795,000.00″

I also found a great list of (all?) the possible ways to format using string.Format() that I can highly recommend here: http://blog.stevex.net/string-formatting-in-csharp/

Free Windows Phone 7 Toolkit e-book

Today I found a new e-book, that I just have to recommend, which explains the Windows Phone 7 Toolkit in depth. It looks really good, covering most controls in detail, and giving some nice examples. This will probably be a good complement to the examples that comes with the toolkit. The book is written by Boryana Miloshevska, an experienced software developer. It can be downloaded for free here: http://www.windowsphonegeek.com/WPToolkitBook

Update: I can also recommend the free e-book “Programming Windows Phone 7″ by Charles Petzold (http://www.charlespetzold.com/phone/) which is also a great resource for WP7 development.

Multiple selection in Listpicker

I thought I would write a quick how-to about how to use the ListPicker for selection of multiple items.

First create a ListPicker in XAML with a few deafult strings like below:

<toolkit:ListPicker Name="myListPicker" Header="Countries" FullModeHeader="CHOOSE COUNTRIES"
SelectionChanged="myListPicker_SelectionChanged" SelectionMode="Multiple">
   <sys:String>Denmark</sys:String>
   <sys:String>Finland</sys:String>
   <sys:String>Norway</sys:String>
   <sys:String>Sweden</sys:String>
</toolkit:ListPicker>

Notice the attribute SelectionMode=”Multiple”, which sets that the user can select multiple items, we also added an event handler for the SelectionChanged event.
Note: To use < sys:string > you need to add xmlns:sys=”clr-namespace:System;assembly=mscorlib” at the top in the < phone:PhoneApplicationPage> tag.

Now in the construct for the page, add the following code:

myListPicker.SummaryForSelectedItemsDelegate = SummarizeItems;

This assigns the function SummarizeItems to summarize the string to be shown for the selected items. Let’s create that function now:

private string SummarizeItems(IList items)
{
   if(items != null && items.Count > 0)
   {
      string summarizedString = "";
      for(int i = 0; i < items.Count;i++)
      {
         summarizedString += (string)items[i];
 
         // If not last item, add a comma to seperate them
         if(i != items.Count - 1)
            summarizedString += ", ";
      }
 
      return summarizedString;
   }
   else
      return "Nothing selected";
}

This function gets an IList with all selected items and we just loop through them and add to a string with a comma between every selected item. Then we return the string, which will be shown when the list picker is in normal mode.

Hope you enjoyed this quick how-to!