Search code examples
c#uwpwindows-store-appswindows-store

Get price of in-app product as a double


How can I get a price of a consumable product from c# code? I need this as a double, but the StorePrice object seems to only offer FormattedPrice as a string (e.g. "42,00 $"):

var storeProducts = await this.storeContext.GetStoreProductsAsync(new[] { "UnmanagedConsumable" }, new[] { "xyz" });
var iap = storeProducts.Products["xyz"];
var price = iap.Price;
price.FormattedPrice ...

I need to get the price as double so I can calculate how much cheaper one IAP is compared to the other.

It looks like I will have to parse the amount out of the formatted string, but that just feels stupid. Are there any other options?


Solution

  • There is no API which provides the price as a pure number. I think parsing the string is not the worst solution, you could use NumberStyles.Currency to make this easier:

    decimal d = decimal.Parse(price.FormattedPrice, NumberStyles.Currency);
    

    However you need to make sure you are actually parsing the string under the correct culture, so that the Parse method can deal with it. You should definitely also wrap this in an exception handler to have an alternate solution when the parsing fails.

    The currency should match the user's current region setting. You can get this using GlobalizationRegion:

    var geographicRegion = new Windows.Globalization.GeographicRegion();
    var code = geographicRegion.CodeTwoLetter;