简体   繁体   English

试图将 iOS 13 的字符串转换为十进制

[英]trying to convert a string to a decimal for iOS 13

I have an app that this was all working correctly before iOS 13. I've checked a few posts but they seem to be saying what I have already done.我有一个应用程序,它在 iOS 13 之前一切正常。我检查了一些帖子,但他们似乎在说我已经做过的事情。

I'm passing a string that has currency symbols and formatting, and I want to strip that and use the string value.我正在传递一个包含货币符号和格式的字符串,我想去掉它并使用字符串值。

func changeCurrencyToDecimal(stringNumber:String) -> Decimal {


    let numberFormatter = NumberFormatter()

    // Pull apart the components of the user's locale
    var locComps = Locale.components(fromIdentifier: Locale.current.identifier)
    // Set the specific currency code
    locComps[NSLocale.Key.currencyCode.rawValue] = options?.currencyCode // or any other specific currency code
    // Get the updated locale identifier
    let locId = Locale.identifier(fromComponents: locComps)
    // Get the new custom locale
    //numberFormatter.locale = Locale(identifier: locId)

    if(options?.currencyCode == nil) {
           print("There is no currency code so use the default locale")
           numberFormatter.locale = Locale.current
       }
       else{
           print("We have a currency code!")
           numberFormatter.locale = Locale(identifier: locId)
       }

    numberFormatter.numberStyle = .currency
    numberFormatter.currencySymbol = ""
    numberFormatter.decimalSeparator = ","  

    print("\(stringNumber) is the number being passed")

    let number = numberFormatter.number(from: stringNumber)

    // check to see if the number is nil and if so, return 0.

    print("\(number) is the number converted")
    if number == nil{
        return 0
    }
    else{
        print("\(number!) is the number")
        let amount = number?.decimalValue
        return amount!
    }

}

An example of a string that I am passing: $300.00 the $ always triggers a return of nil.我传递的字符串示例:$300.00 $ 总是触发返回 nil。 If I pass 300.00, then the converter works just fine.如果我通过 300.00,那么转换器就可以正常工作。 But I need to be able to check for any currency the user has set for their device.但我需要能够检查用户为其设备设置的任何货币。

The currency codes that I am checking are the ones Apple supplies in the var currencyCode: String? { get }我正在检查的货币代码是 Apple 在var currencyCode: String? { get } var currencyCode: String? { get } options is just where I am storing those codes. var currencyCode: String? { get } options 就是我存储这些代码的地方。

The numberFormatter produces nil every time, and it seems because my decimal is not being stripped of its formatting. numberFormatter 每次都产生 nil ,这似乎是因为我的小数没有被剥夺其格式。

Again this worked before iOS 13, so I am guessing something changed on Apple's side and I just might not have come acrossed it yet.这在 iOS 13 之前再次起作用,所以我猜苹果方面发生了一些变化,我可能还没有遇到过。

UPDATE Here is a user seniaro.更新这是一个用户seniaro。 The user enters an amount.用户输入金额。 If the user hits save right away, I take the amount and convert it into a decimal to save in coredata.如果用户立即点击保存,我会将金额转换为小数以保存在 coredata 中。 But if the user dismisses the keyboard, I take that amount and convert it into a string to display the currency symbol.但是,如果用户关闭键盘,我会获取该金额并将其转换为字符串以显示货币符号。 I allow the user to set their currency, so using the locale doesn't work for me, as sometimes they are not using that system.我允许用户设置他们的货币,所以使用区域设置对我不起作用,因为有时他们不使用该系统。 So after the keyboard is dismissed and their amount is displayed with the correct formatting, I have it programmed that if they would happen to change their amount and once again dismiss the keyboard, and the same steps are repeated.因此,在关闭键盘并以正确的格式显示其数量后,我对其进行了编程,如果他们碰巧改变了数量并再次关闭键盘,并重复相同的步骤。 The currency symbols are stripped and the string is converted into a decimal.货币符号被剥离,字符串被转换为小数。

I hope this better gives you an idea of how this works in my app.我希望这能更好地让您了解它在我的应用程序中的工作原理。

UPDATE I've added an if/else statement to see if the locale has been set or if it comes back nil, and if so to set it to the Locale.current更新我添加了一个 if/else 语句来查看语言环境是否已设置或者是否返回 nil,如果是,则将其设置为 Locale.current

Assuming the input string is using the device's current locale you can create a NumberFormatter with Locale.current and set the numberStyle to currency.假设输入字符串使用设备的当前语言环境,您可以使用numberStyle创建NumberFormatter并将Locale.current设置为货币。

I used a small playground to test it:我用一个小操场来测试它:

import Foundation

func changeCurrencyToDecimal(_ stringNumber: String) -> Decimal? {
  let numberFormatter = NumberFormatter()
  numberFormatter.numberStyle = .currency

  let number = numberFormatter.number(from: stringNumber)
  return number?.decimalValue
}

let numbersFormatted = ["$300", "$3,000.04", "3.000,04", "Wazaa", "3000000"]
numbersFormatted
  .map { changeCurrencyToDecimal($0) }
  .forEach { print($0 ?? "Not a value") }

This prints:这打印:

  • 300 300
  • 3000.04 3000.04
  • Not a value不是一个值
  • Not a value不是一个值
  • 3000000 3000000

If you keep getting nil as a result your device's locale is different as the sample string number you are using in the question ($300).如果您因此一直得到nil ,则您的设备的语言环境与您在问题中使用的示例字符串编号不同(300 美元)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM