我正在尝试通过Eureka表单实现计算器。
在小数行中,它允许输入多个小数点。我想将其限制为仅一个小数点。例如,我可以输入“ 3 .... 5”。但是我只想输入“ 3.5”。
这是我的代码:
<<< DecimalRow() {
$0.tag = Tags.term
$0.title = "Term (Years)"
$0.baseCell.accessibilityLabel = $0.title
$0.formatter = DecimalFormatter()
$0.placeholder = "Years"
$0.add(rule: RuleRequired(msg: "Term field is required."))
$0.add(rule: RuleGreaterThan(min: 0, msg: "Term must be
greater than zero."))
$0.useFormatterDuringInput = true
$0.validationOptions = .validatesOnDemand
// DecimalFormatter
class DecimalPercentageFormatter: NumberFormatter {
init(minimumFractionDigits: Int = 2, maximumFractionDigits: Int = 2) {
super.init()
self.minimumFractionDigits = minimumFractionDigits
self.maximumFractionDigits = maximumFractionDigits
locale = Locale.current
numberStyle = .percent
positiveSuffix = "%"
negativeSuffix = "%"
}
}