Swift playground -如何将带有逗号的字符串转换为带有小数的字符串

8e2ybdfx  于 2023-03-11  发布在  Swift
关注(0)|答案(6)|浏览(151)

我刚进入雨燕的世界。
如何将一个带逗号的字符串转换为带小数点的字符串?
代码可以很好地使用点(.)
问题是当我使用逗号(,)...时:可变价格
问题的根源是十进制法语键盘使用逗号(,)而不是点(.)
不清楚如何使用NSNumberFormattergeneratesDecimalNumbers(如果它是键)。可能有多个选项。

//The answer change if "2,25" or "2.25" is used.

var price      : String = "2,25"
var priceFloat = (price as NSString).floatValue

//I need to have 2.25 as answer.

var costString = String(format:"%.2f", priceFloat)

感谢您的时间和您的帮助!

roejwanj

roejwanj1#

update:Xcode 8.2.1 · Swift 3.0.2您可以使用NumberFormatter()将字符串转换为数字,只需指定decimalSeparator,如下所示:

extension String {
    static let numberFormatter = NumberFormatter()
    var doubleValue: Double {
        String.numberFormatter.decimalSeparator = "."
        if let result =  String.numberFormatter.number(from: self) {
            return result.doubleValue
        } else {
            String.numberFormatter.decimalSeparator = ","
            if let result = String.numberFormatter.number(from: self) {
                return result.doubleValue
            }
        }
        return 0
    }
}

"2.25".doubleValue // 2.25
"2,25".doubleValue // 2.25

let price = "2,25"
let costString = String(format:"%.2f", price.doubleValue)   // "2.25"

您还应该使用NumberFormat进行货币格式设置,因此创建一个只读计算属性currency extending FloatingPoint协议,以从String doubleValue属性返回格式化字符串。
x一个一个一个一个x一个一个二个一个x一个一个三个一个

5ktev3wc

5ktev3wc2#

您可以为此使用Swift 3

let currentAmount = "2,50"

currentAmount = currentAmount.replacingOccurrences(of: ",", with: ".")

print(currentAmount) // "2.50\n"
w1e3prcc

w1e3prcc3#

var price = "2,25"
price = price.replacingOccurrences(of: ",", with: ".")
var priceFloat = (price as NSString).floatValue
f4t66c6m

f4t66c6m4#

可为空的扩展版本:

extension String 
{
    static let customNumberFormatter = NumberFormatter()
    var doubleValue: Double? {
        String.customNumberFormatter.decimalSeparator = "."
        if let result =  String.customNumberFormatter.number(from: self) {
            return result.doubleValue
        } else {
            String.customNumberFormatter.decimalSeparator = ","
            if let result = String.customNumberFormatter.number(from: self) {
                return result.doubleValue
            }
        }
        return nil
    }
}
thigvfpy

thigvfpy5#

接受答案的简化版本,出于安全原因,我还将numberFormatter设置为私有。

extension String {
    private static let numberFormatter = NumberFormatter()

    var doubleValue: Double {
        String.numberFormatter.decimalSeparator = "."
    
        if let result = String.numberFormatter.number(from: self) {
            return result.doubleValue
        }
        String.numberFormatter.decimalSeparator = ","
        return String.numberFormatter.number(from: self)?.doubleValue ?? 0
    }
}
eimct9ow

eimct9ow6#

编辑:更新以与当前版本的Swift配合使用:

let amount = "8,35"

var counter: Int = 0
var noCommaNumber: String!
for var carattere in (amount) {
    if carattere == "," { carattere = "." }
    if counter != 0 { noCommaNumber = "\(noCommaNumber ?? "\(carattere)")" + "\(carattere)" } else { noCommaNumber = "\(carattere)" } // otherwise first record will always be nil
    counter += 1
}

let importo = Float(noCommaNumber)

相关问题