如何在iOS Swift 5中对多个欧元值进行总值排列,例如[“ 179.95英镑”,“ 199.95英镑”,“ 89.95英镑”] [英] How To multiple Euro values total arrays in ios Swift 5 Like ["£179.95", "£199.95", "£89.95"]

查看:115
本文介绍了如何在iOS Swift 5中对多个欧元值进行总值排列,例如[“ 179.95英镑”,“ 199.95英镑”,“ 89.95英镑”]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

array = [ 179.95英镑, 199.95英镑, 89.95英镑]

array = ["£179.95", "£199.95", "£89.95"]

如何将此值乘以ios即可得到总值Swift 4 。

How to multiple this values add get the total values in ios swift 4.

请帮助我。

推荐答案

如果确定确保数组中包含的字符串始终以£开头,您可以这样做:

If you're sure that the strings contained in your array always start with a £, you could do this:

let sum = array.compactMap { Double($0.replacingOccurrences(of: "£", with: "")) }
               .reduce(0.0, { $0 + $1 })

示例:

let array = ["£179.95", "£199.95", "£89.95"]
let sum = array.compactMap { Double($0.replacingOccurrences(of: "£", with: "")) }
               .reduce(0.0, { $0 + $1 })
print(sum) // 469.84999999999997

这篇关于如何在iOS Swift 5中对多个欧元值进行总值排列,例如[“ 179.95英镑”,“ 199.95英镑”,“ 89.95英镑”]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆