How to input currency format on a text field (from right to left) using Swift?

前端 未结 9 1040
甜味超标
甜味超标 2020-11-22 01:57

I have a number let’s say 0.00.

  • When the user taps 1. We should have 0.01
  • When the user taps 2. We should display 0.
9条回答
  •  囚心锁ツ
    2020-11-22 02:25

    My final code thanks for your help

    extension Double {
                var twoDigits: Double {
                    let nf = NSNumberFormatter()
                    nf.numberStyle = NSNumberFormatterStyle.DecimalStyle
                    nf.minimumFractionDigits = 2
                    nf.maximumFractionDigits = 2
                    return self
                }
        }
        var cleanText:String!
                let number:String = sender.currentTitle as String!
                if(amountDisplay.text != nil)
                {
                    cleanText = String(Array(amountDisplay.text!).map{String($0)}.filter{ $0.toInt() != nil }.map{Character($0)} ) as String
                    cleanText = cleanText + number
                }else{
                    cleanText = number
                }
    
                amount = (Double(cleanText.toInt()!) / 100).twoDigits
                formatter.locale = NSLocale(localeIdentifier: currencies[current_currency_index])
                amountDisplay.text = "\(formatter.stringFromNumber(amount!)!)"
    

提交回复
热议问题