如何从 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 与另一个 IAP 相比便宜多少。
看起来我必须从格式化的字符串中解析出数量,但这感觉很愚蠢。还有其他选择吗?
没有以纯数字形式提供价格的 API。我认为解析字符串并不是最糟糕的解决方案,您可以使用它NumberStyles.Currency
来简化此操作:
decimal d = decimal.Parse(price.FormattedPrice, NumberStyles.Currency);
但是,您需要确保您实际上是在正确的区域性下解析字符串,以便该Parse
方法可以处理它。您绝对还应该将其包装在异常处理程序中,以便在解析失败时提供替代解决方案。
货币应与用户当前的区域设置相匹配。你可以使用GlobalizationRegion
:
var geographicRegion = new Windows.Globalization.GeographicRegion();
var code = geographicRegion.CodeTwoLetter;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句