技术问题:product.priceFormatStyle返回PLN而非本地货币符号zł
解决Product价格格式化显示国际货币代码而非本地符号的问题
问题分析
你代码中product.priceFormatStyle.format(yearlyPricePerWeek)输出PLN(波兰兹罗提的国际代码),但product.displayPrice能正常显示本地符号zł,核心原因是**priceFormatStyle默认的货币展示配置与displayPrice不一致**:displayPrice内部采用了本地符号的格式化规则,而默认的priceFormatStyle使用了currencyPresentation(.code)的配置。
解决方案
方案1:调整priceFormatStyle的货币展示方式
直接修改priceFormatStyle的currencyPresentation属性,指定使用本地符号,这是最稳定的原生方案:
var title3: String { switch ProductID(rawValue: product.id) { case .yearly: let yearlyPricePerWeek: Decimal = product.price / 52 // 调整格式器为显示本地货币符号 let localizedFormatStyle = product.priceFormatStyle.currencyPresentation(.localSymbol) return "\(localizedFormatStyle.format(yearlyPricePerWeek)) per week" case .weekly: return "\(product.displayPrice) per week" case .weeklyWithTrial: return "then \(product.displayPrice) per week" case .none: return "" } }
如果需要适配用户系统的当前地区,可同时指定跟随系统Locale:
let localizedFormatStyle = product.priceFormatStyle .locale(Locale.current) .currencyPresentation(.localSymbol)
方案2:手动构建自定义格式化器
完全自定义规则,确保显示指定的本地符号:
var title3: String { switch ProductID(rawValue: product.id) { case .yearly: let yearlyPricePerWeek: Decimal = product.price / 52 let formatter = NumberFormatter() formatter.numberStyle = .currency // 指定波兰Locale以显示zł,若需多地区适配则用Locale.current formatter.locale = Locale(identifier: "pl_PL") formatter.currencyCode = "PLN" if let formattedPrice = formatter.string(from: yearlyPricePerWeek as NSDecimalNumber) { return "\(formattedPrice) per week" } return "" case .weekly: return "\(product.displayPrice) per week" case .weeklyWithTrial: return "then \(product.displayPrice) per week" case .none: return "" } }
关键说明
currencyPresentation的可选值:.code:显示国际货币代码(如PLN).symbol:显示通用货币符号(如zł).localSymbol:显示本地化的货币符号(优先适配用户系统地区的符号)
- 优先选择方案1,基于StoreKit原生扩展实现,更符合系统规范且稳定性更高。
内容的提问来源于stack exchange,提问作者pvllnspk
相关产品推荐
相关产品推荐

