尝试在 Swift 中组合数字和变量
我是一个新手,试图通过将数字和变量组合在一起来创建 0.2。但我想我已经把一切都倒退了。有人可以帮忙吗?
//将字符串“20%”转换为“2”。
let tipPercent = tip.prefix(1)
//将字符串转换为 Int '2'
let tipPercent1: Int = Int(tipPercent) ?? 0
//所以现在我想将 0. 与变量tipPercent1结合起来。这将创建“0.2”
let twentyTipamount = (billamount * 0.tipPercent1) + billamount
I am a newbie trying hard to create 0.2 from combining a a number and a variable together. But I think I have it all backwards somehow. can anyone help?
//turns a string of '20%' into '2'.
let tipPercent = tip.prefix(1)
//turns the string into a Int of '2'
let tipPercent1: Int = Int(tipPercent) ?? 0
//So now I want to combine the 0. with the variable tipPercent1. this will create '0.2'
let twentyTipamount = (billamount * 0.tipPercent1) + billamount
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您只想将百分比字符串解析为数字。您应该使用
NumberFormatter
。It seems like you just want to parse a percent string into a number. You should use a
NumberFormatter
.