gpt4 book ai didi

swift - 将字符串转换为数字、格式;除非不能转换,则 string=string

转载 作者:行者123 更新时间:2023-11-28 12:52:29 25 4
gpt4 key购买 nike

变量 benefit.amount 以字符串形式出现。

var amount = String()
if let tempAmount = benefit.amount as? NSDecimalNumber {
amount = Formatter.currencyFormat(tempAmount)
} else {
amount = tempAmount
}

我想做的是,如果 benefit.amount 类似于 1000,我可以在其上使用货币格式化程序。虽然有时该变量可能看起来像 Included in Policy Year Maximum Benefit,但显然不能变成数字。

请指教。

最佳答案

只需尝试将值转换为 Int。初始化程序在失败时返回 nil

let amount : String
if let tempAmount = Int(benefit.amount) {
amount = Formatter.currencyFormat(tempAmount)
} else {
amount = benefit.amount
}

关于swift - 将字符串转换为数字、格式;除非不能转换,则 string=string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36044007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com