Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xcode 无小数点的心率_Xcode_Apple Watch_Healthkit - Fatal编程技术网

Xcode 无小数点的心率

Xcode 无小数点的心率,xcode,apple-watch,healthkit,Xcode,Apple Watch,Healthkit,我正在用Xcode为Apple watch做一个watch应用程序,来自Apple开发者网站的示例代码将心率四舍五入到小数点后一位,例如61.0 我怎样才能解决这个问题 case HKQuantityType.quantityType(forIdentifier: .heartRate): /// - Tag: SetLabel let heartRateUnit = HKUnit.count().unitDivided(by: H

我正在用Xcode为Apple watch做一个watch应用程序,来自Apple开发者网站的示例代码将心率四舍五入到小数点后一位,例如61.0 我怎样才能解决这个问题

case HKQuantityType.quantityType(forIdentifier: .heartRate):
                /// - Tag: SetLabel
                let heartRateUnit = HKUnit.count().unitDivided(by: HKUnit.minute())
                let value = statistics.mostRecentQuantity()?.doubleValue(for: heartRateUnit)
                let roundedValue = Double( round( 1 * value! ) / 1 )
                label.setText("\(roundedValue) BPM")
我尝试将其中的两个1都更改为0,但这给了我6.1 BPM或0.0 BPM


谢谢

一个简单的解决方案是四舍五入到整数并显示整数

let value = // ... some Double ...
let s = String(Int(value.rounded(.toNearestOrAwayFromZero)))
label.setText(s + " BPM")
但是,正确地说,您应该将基于数字的字符串格式化交给NumberFormatter。这就是它的工作:格式化一个数字


你在试着编一条线。使用数字格式。谢谢,我这样做了,效果很好。我可能添加了我不应该添加的内容,但它的工作原理是:让heartRateUnit=HKUnit.count().unitDivided(by:HKUnit.minute())让value=statistics.mostRecentQuantity()?.doubleValue(for:heartRateUnit)让roundedValue=Double(round(1*value!)/1)let valueA=roundedValue let s=String(Int(valueA.rounded(.toNearestOrAwayFromZero))label.setText(s+“BPM”)
let i = value.rounded(.toNearestOrAwayFromZero)
let nf = NumberFormatter()
nf.numberStyle = .none
let s = nf.string(from: i as NSNumber)!
// ... and now show the string