获取应用内商品的双倍价格
Get price of in-app product as a double
如何从 C# 代码中获取消耗品的价格?我需要它作为双精度数,但 StorePrice
对象似乎只提供 FormattedPrice
作为字符串(例如“42,00 $”):
var storeProducts = await this.storeContext.GetStoreProductsAsync(new[] { "UnmanagedConsumable" }, new[] { "xyz" });
var iap = storeProducts.Products["xyz"];
var price = iap.Price;
price.FormattedPrice ...
我需要将价格翻倍,这样我才能计算出一个 IAP 与另一个相比便宜多少。
看来我必须从格式化字符串中解析出金额,但这感觉很愚蠢。还有其他选择吗?
没有 API 以纯数字形式提供价格。我认为解析字符串并不是最糟糕的解决方案,您可以使用 NumberStyles.Currency
使这更容易:
decimal d = decimal.Parse(price.FormattedPrice, NumberStyles.Currency);
但是您需要确保您实际上是在正确的区域性下解析字符串,以便 Parse
方法可以处理它。您绝对还应该将其包装在异常处理程序中,以便在解析失败时获得替代解决方案。
货币应与用户当前的区域设置相匹配。你可以使用 GlobalizationRegion
:
var geographicRegion = new Windows.Globalization.GeographicRegion();
var code = geographicRegion.CodeTwoLetter;
如何从 C# 代码中获取消耗品的价格?我需要它作为双精度数,但 StorePrice
对象似乎只提供 FormattedPrice
作为字符串(例如“42,00 $”):
var storeProducts = await this.storeContext.GetStoreProductsAsync(new[] { "UnmanagedConsumable" }, new[] { "xyz" });
var iap = storeProducts.Products["xyz"];
var price = iap.Price;
price.FormattedPrice ...
我需要将价格翻倍,这样我才能计算出一个 IAP 与另一个相比便宜多少。
看来我必须从格式化字符串中解析出金额,但这感觉很愚蠢。还有其他选择吗?
没有 API 以纯数字形式提供价格。我认为解析字符串并不是最糟糕的解决方案,您可以使用 NumberStyles.Currency
使这更容易:
decimal d = decimal.Parse(price.FormattedPrice, NumberStyles.Currency);
但是您需要确保您实际上是在正确的区域性下解析字符串,以便 Parse
方法可以处理它。您绝对还应该将其包装在异常处理程序中,以便在解析失败时获得替代解决方案。
货币应与用户当前的区域设置相匹配。你可以使用 GlobalizationRegion
:
var geographicRegion = new Windows.Globalization.GeographicRegion();
var code = geographicRegion.CodeTwoLetter;