gpt4 book ai didi

ios - 手动格式化

转载 作者:行者123 更新时间:2023-11-28 06:16:20 25 4
gpt4 key购买 nike

Swift 新手,当我输入 .或者在 .0 或 .00 之后它应该反射(reflect)为 0.00,但这对 myTxtField.text 是不可见的。

以下是我的代码-

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
// return NO to not change text
//Decimal
switch string {
case "0","1","2","3","4","5","6","7","8","9",".":
currentString += string
print(currentString)
formatCurrency(currentString)

default:
if string.characters.count == 0 && currentString.characters.count != 0 {
currentString = String(currentString.characters.dropLast())
formatCurrency(currentString)
}
}

return false
}

func formatCurrency(string: String) {

let formatter = NSNumberFormatter()
formatter.numberStyle = NSNumberFormatterStyle.DecimalStyle
formatter.maximumFractionDigits = 2
formatter.roundingMode = .RoundDown
formatter.locale = NSLocale(localeIdentifier: "en_US")
let numberFromField = (NSString(string: currentString).doubleValue)
let tAmount:String!
if string == "" {
myTxtField.text = ""
tAmount = "$" + formatter.stringFromNumber(numberFromField)!
}else {
myTxtField.text = "$" + formatter.stringFromNumber(numberFromField)!
tAmount = "$" + formatter.stringFromNumber(numberFromField)!
}
print("value=\(HManager.convertCurrencyToDouble(tAmount))")

}

最佳答案

以下是使用 shouldChangeCharactersIn 委托(delegate)方法和 editingDidEnd 实现您的目标的示例。

  • shouldChangeCharactersIn 将文本字段限制为仅接受数字和小数点
  • editingDidEnd 是用 2 位十进制数字格式化文本字段。

enter image description here

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var textField: UITextField!

override func viewDidLoad() {
super.viewDidLoad()

textField.delegate = self
}

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

let allowedCharacters = CharacterSet.decimalDigits

if string.rangeOfCharacter(from: allowedCharacters.inverted) != nil {
// If the added character is not a number
if string == "." {
// If the added character is the decimal point
let countDots = textField.text!.components(separatedBy: ".").count - 1
if countDots == 0 {
return true
}
}
return false
}
return true
}

@IBAction func editingDidEnd(_ sender: UITextField) {
// Format the input with 2 decimal digits.
if let input = sender.text, let inputDouble = Double(input) {
sender.text = String(format: "%.2f", inputDouble)
} else {
sender.text = "0.00"
}
}
}

更新

为了添加货币符号 ($) 并在需要时删除小数部分,您需要一个函数来检查输入是 double 还是 Int .

@IBAction func editingDidEnd(_ sender: UITextField) {
// Format the input with 2 decimal digits.
if let input = sender.text, let number = Double(input) {
if canConvertToInt(number) {
sender.text = "$\(Int(number))"
} else {
sender.text = "$\(number)"
}
} else {
sender.text = "$0"
}
}

// Check if the number is not decimal
func canConvertToInt(_ double: Double) -> Bool {
return rint(double) == double
}

关于ios - 手动格式化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45036963/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com