如何实现 NumberFormatter
How to implement NumberFormatter
我想让我的计数器只显示小数点后八位。最后一位数字一旦变为 0 就会消失。试图让尾随零显示自己。
有人告诉我使用 NumberFormatter
。我如何获得它以便 secondUpdateLabel 继承格式化程序?
var secondTimer = Timer()
var secondCounter: Double = 70.000000
var secondDecreaser: Double = 0.000001
func secondStartCounting(initialNumber x: Double, decreaseBy y: Double) {
self.secondCounter = x
self.secondDecreaser = y
self.secondTimer.invalidate()
let secondTimer = Timer(timeInterval: 0.38, target: self, selector: #selector(ViewController.secondUpdateTimer), userInfo: nil, repeats: true)
RunLoop.main.add(secondTimer, forMode: .commonModes)
self.secondTimer = secondTimer
}
@objc func secondUpdateTimer() {
secondCounter -= secondDecreaser
let secondUpdateLabel = secondCounter
let formatter = NumberFormatter()
formatter.numberStyle = NumberFormatter.Style.decimal
formatter.usesSignificantDigits = true
formatter.minimumFractionDigits = 8
formatter.maximumFractionDigits = 8
// not proper code next. just want to know how to get secondUpdateLabel to inherit the properties I applied to formatter. not sure what the next steps are
secondStat.text =
}
这对我有用!
var secondTimer = Timer()
var secondCounter: Double = 70.000000
var secondDecreaser: Double = 0.000001
func secondStartCounting(initialNumber x: Double, decreaseBy y: Double) {
self.secondCounter = x
self.secondDecreaser = y
self.secondTimer.invalidate()
let secondTimer = Timer(timeInterval: 0.0000001, target: self, selector: #selector(ViewController.secondUpdateTimer), userInfo: nil, repeats: true)
RunLoop.main.add(secondTimer, forMode: .commonModes)
self.secondTimer = secondTimer
}
@objc func secondUpdateTimer() {
secondCounter -= secondDecreaser
let secondUpdateLabel = secondCounter
let formatter = NumberFormatter()
formatter.numberStyle = NumberFormatter.Style.decimal
formatter.usesSignificantDigits = false
formatter.minimumFractionDigits = 8
formatter.maximumFractionDigits = 8
secondStat.text = formatter.string(from: NSNumber(value: secondUpdateLabel))
}
发现我必须将 usesSignificantDigits 设置为 false,我实现的最后一行代码解决了我的问题。
我想让我的计数器只显示小数点后八位。最后一位数字一旦变为 0 就会消失。试图让尾随零显示自己。
有人告诉我使用 NumberFormatter
。我如何获得它以便 secondUpdateLabel 继承格式化程序?
var secondTimer = Timer()
var secondCounter: Double = 70.000000
var secondDecreaser: Double = 0.000001
func secondStartCounting(initialNumber x: Double, decreaseBy y: Double) {
self.secondCounter = x
self.secondDecreaser = y
self.secondTimer.invalidate()
let secondTimer = Timer(timeInterval: 0.38, target: self, selector: #selector(ViewController.secondUpdateTimer), userInfo: nil, repeats: true)
RunLoop.main.add(secondTimer, forMode: .commonModes)
self.secondTimer = secondTimer
}
@objc func secondUpdateTimer() {
secondCounter -= secondDecreaser
let secondUpdateLabel = secondCounter
let formatter = NumberFormatter()
formatter.numberStyle = NumberFormatter.Style.decimal
formatter.usesSignificantDigits = true
formatter.minimumFractionDigits = 8
formatter.maximumFractionDigits = 8
// not proper code next. just want to know how to get secondUpdateLabel to inherit the properties I applied to formatter. not sure what the next steps are
secondStat.text =
}
这对我有用!
var secondTimer = Timer()
var secondCounter: Double = 70.000000
var secondDecreaser: Double = 0.000001
func secondStartCounting(initialNumber x: Double, decreaseBy y: Double) {
self.secondCounter = x
self.secondDecreaser = y
self.secondTimer.invalidate()
let secondTimer = Timer(timeInterval: 0.0000001, target: self, selector: #selector(ViewController.secondUpdateTimer), userInfo: nil, repeats: true)
RunLoop.main.add(secondTimer, forMode: .commonModes)
self.secondTimer = secondTimer
}
@objc func secondUpdateTimer() {
secondCounter -= secondDecreaser
let secondUpdateLabel = secondCounter
let formatter = NumberFormatter()
formatter.numberStyle = NumberFormatter.Style.decimal
formatter.usesSignificantDigits = false
formatter.minimumFractionDigits = 8
formatter.maximumFractionDigits = 8
secondStat.text = formatter.string(from: NSNumber(value: secondUpdateLabel))
}
发现我必须将 usesSignificantDigits 设置为 false,我实现的最后一行代码解决了我的问题。