Using Swift how to convert a string to a number

一个人想着一个人 提交于 2021-01-02 02:22:30

问题


I am getting values back from a web service that gives me back prices in a string format, this is put into a Dictionary, so I get prices back as "1.5000" for example, which is obviously 1.50 in currency. However for the life of me I cannot get anything to work in Swift to format this correctly. In most other languages you can do this in a couple of seconds, so I'm getting a bit frustrated with something that is so simple.

Here's my test code:

    var testnumber = "1.5000"
    let n = NSNumberFormatter()
    n.numberStyle = NSNumberFormatterStyle.DecimalStyle
    n.maximumFractionDigits = 2
    n.minimumFractionDigits = 2
    let returnNumber = n.numberFromString(testnumber)
    println("Returned number is \(returnNumber)")

This prints out in debug "number is Optional(1.5)" not 1.50!

I have changed NSNumberFormatterStyle.DecimalStyle to NSNumberFormatterStyle.CurrencyStyle as I thought that may do it for me as the returned number is a currency anyway, but that gives me back in debug "Returned number is nil" - which is even more confusing to me!

I have tried using maximumIntegerDigits and minimumIntegerDigits, setting locales using n.locale = NSLocale.currentLocale(), setting formatWidth, setting paddingPosition and paddingCharacter but nothing helps, I either get nil back to 1.5.

All I ultimately need to do is convert a string to a float or a currency value, and ensure there are 2 decimal places, and I can't believe it's this hard to accomplish!

Any help would be very gratefully received.


回答1:


You are printing a number not a string

Xcode 11.4 • Swift 5.2 or later

extension Formatter {
    static let usCurrency: NumberFormatter = {
        let formatter = NumberFormatter()
        formatter.locale = .init(identifier: "en_US")
        formatter.numberStyle = .currency
        return formatter
    }()
}

extension String {
    var double: Double? { Double(self) }
    var usCurrencyFormatted: String {
        Formatter.usCurrency.string(for: double) ?? Formatter.usCurrency.string(for: 0) ?? ""
    }
}

"1.1222".usCurrencyFormatted           // "$1.12"
"2".usCurrencyFormatted                // "$2.00"



回答2:


The problem is about numberFromString returning an optional - so you have to unwrap before printing. Just to be safe, you can use optional binding:

if let returnNumber = n.numberFromString(testnumber) {
    println("Returned number is \(returnNumber)")
}

otherwise if it's ok for the app to crash if the optional is nil (in some cases this is a wanted behavior if the optional is expected to always contain a non nil value) just use forced unwrapping:

let returnNumber = n.numberFromString(testnumber)!
println("Returned number is \(returnNumber)")

That fixes the unwanted "Optional(xx)" text. As for formatting a float/double number, there are probably several ways of doing it - the one I would use is c-like string formatting, available via NSString:

let formattedNumber = NSString(format: "%.2f", returnNumber)
println("Returned number is \(formattedNumber)")

Use String Format Specifiers as reference if you want to know more about format specifiers.




回答3:


You could probably just use the NSNumberFormatter that you just created.

let returnNumber = n.stringFromNumber(n.numberFromString(testnumber))

returnNumber will now be of type String.




回答4:


The following returns to 2 decimal places for me in playgrounds. May be of some help to you. Uses NSNumberFormatter and then unwraps the optional

let testnumber: String = "1.50000"
let numberFormatter = NSNumberFormatter()
let number  = numberFormatter.numberFromString(testnumber)
if let final = number?.floatValue {
    println("Returned number is " + String(format: "%.2f", final))
} 


来源:https://stackoverflow.com/questions/27991601/using-swift-how-to-convert-a-string-to-a-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!