1

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?

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
Tomáš Bezouška
  • 1,395
  • 1
  • 10
  • 25

1 Answers1

1

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;
Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
  • I tried that, but I am not sure which culture to use or whether it depends on windows locale. – Tomáš Bezouška Jun 14 '18 at 18:17
  • I have updated my answer, I think the `GeographicRegion` should match the currency – Martin Zikmund Jun 15 '18 at 05:34
  • Turns out the Region api is not needed, getting culture via `CultureInfo.CurrentCulture` works, BUT you first need to remove the `;nbsp` symbol from the formatted string (replace is with regular space), then parsing will work. Accepting your answer – Tomáš Bezouška Jun 18 '18 at 05:52
  • This is so sad, why does the API only return the formatted price and not the raw number...? – Dominic Apr 05 '19 at 15:27