【问题标题】:Convert string into currency number format, with, without or with 0 decimal places将字符串转换为货币数字格式,带、不带或带 0 个小数位
【发布时间】:2021-01-01 17:04:06
【问题描述】:

我有一个字符串,它总是转换成 this 这样的字符串,用户输入一个数字并始终从小数位开始,

所以我有 0.01 -> 0.10 -> 1.00

但我不想要这样的东西,我只想转换用户输入的内容

这是我现有的将 100000 转换为 1,000.00 的代码

func convertme(string: String) -> String{
    var number: NSNumber!
    let formatter = NumberFormatter()
    formatter.numberStyle = .currencyAccounting
    formatter.currencySymbol = ""
    formatter.maximumFractionDigits = 2
    formatter.minimumFractionDigits = 2

    var amountWithPrefix = string

    // remove from String: "$", ".", ","
    let regex = try! NSRegularExpression(pattern: "[^0-9]", options: .caseInsensitive)
    amountWithPrefix = regex.stringByReplacingMatches(in: amountWithPrefix, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, string.count), withTemplate: "")
    
    print("amountWithPrefix", amountWithPrefix)

    let double = (amountWithPrefix as NSString).doubleValue
    number = NSNumber(value: (double / 100))

    // if first number is 0 or all numbers were deleted
    guard number != 0 as NSNumber else {
        return ""
    }

    return formatter.string(from: number)!
}

预期结果:

我想在不添加额外数据的情况下格式化字符串上的数字,我想把 (100000. 变成 100,000.) (100000.0 变成 100,000.0

我希望我的 100000 转换为 100,000,并且如果用户也输入小数,则只有小数,所以当用户输入 100000.00 时,它将转换为 100,000.00。

PS。我有一个只接受数字但不接受小数的正则表达式,我怎样才能让它也接受小数?

【问题讨论】:

  • @LeoDabus 不,不是,我已经解释了为什么我的预期输出与该链接不同的问题。我在我现有的代码中展示了它,输出是你给出的链接中的答案。

标签: swift number-formatting nsnumberformatter


【解决方案1】:

您可以简单地从原始字符串中过滤非数字或句点,尝试将结果字符串强制转换为整数。如果成功将格式化程序的最大小数位数设置为零,否则将最大小数位数设置为 2 并强制字符串加倍:

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

extension Numeric {
    var currencyUS: String {
         Formatter.currency.string(for: self) ?? ""
    }
}

func convertme(string: String) -> String {
    let string = string.filter("0123456789.".contains)
    if let integer = Int(string)  {
        Formatter.currency.maximumFractionDigits = 0
        return Formatter.currency.string(for: integer) ?? "0"
    }
    Formatter.currency.maximumFractionDigits = 2
    return Double(string)?.currencyUS ?? "0"
}

convertme(string: "100000")     // "100,000"
convertme(string: "100000.00")  // "100,000.00"


编辑/更新: “十万。”它不是有效的数字格式。您需要在字符串末尾手动插入句点。

func convertme(string: String) -> String {
    var string = string.filter("0123456789.".contains)
    // this makes sure there is only one period and keep only the last one in the string
    while let firstIndex = string.firstIndex(of: "."),
          let _ = string[firstIndex...].dropFirst().firstIndex(of: ".") {
        string.remove(at: firstIndex)
    }
    // get the index of the period in your string
    if let index = string.firstIndex(of: ".") {
        // get the fraction digits count and set the number formatter appropriately
        let fractionDigits = string[index...].dropFirst().count
        Formatter.currency.minimumFractionDigits = fractionDigits
        Formatter.currency.maximumFractionDigits = fractionDigits
        // Number Formatter wont add a period at the end of the string if there is no fractional digits then you need to manually add it yourself
        if fractionDigits == 0 {
            return (Double(string)?.currencyUS ?? "0") + "."
        }
    } else {
        // in case there is no period set the fraction digits to zero
        Formatter.currency.minimumFractionDigits = 0
        Formatter.currency.maximumFractionDigits = 0
    }
    return Double(string)?.currencyUS ?? "0"
}

游乐场测试:

convertme(string: "100000")      // "100,000"
convertme(string: "100000.")     // "100,000."
convertme(string: "100000.0")    // "100,000.0"
convertme(string: "100000.00")   // "100,000.00"
convertme(string: "100000.000")  // "100,000.000"

【讨论】:

  • 用户输入 convertme(string: "100000.") 被转换为 100,000.00,我的意思是在我的预期输出中只转换用户输入的内容,所以当用户输入 100000 时,它应该被转换为 100,000。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-16
  • 2017-06-30
  • 1970-01-01
相关资源
最近更新 更多