gpt4 book ai didi

ios - Core Motion 将距离转换为 KM

转载 作者:可可西里 更新时间:2023-11-01 01:55:58 26 4
gpt4 key购买 nike

在我的健康应用程序中显示距离为 0.1 公里,但在我的应用程序中它给出的值为 0.106。我怎样才能把它改成十进制?这是我用来获取当前统计信息的代码:

pedometer.queryPedometerData(from: Date().startOfDay, to: Date()) { data, error in
guard let activityData = data, error == nil else {
print("There was an error getting the data: \(String(describing: error))")
return
}
print((Double(activityData.distance!.intValue)/Double(1000))) // Print out optional distance in of walking and running in meters. / 1000 to convert from meter to km.
}

Print gives me: 0.106

最佳答案

您可以使用 Swift Measurement 类型转换您的长度单位,并使用 MeasurementFormatter 将其显示给用户:

let number: NSNumber = 106
let measurement = Measurement(value: number.doubleValue, unit: UnitLength.meters).converted(to: .kilometers)

let mf = MeasurementFormatter()
mf.unitOptions = .providedUnit
mf.unitStyle = .medium
mf.numberFormatter.maximumFractionDigits = 1
mf.string(from: measurement) // "0.1 km"

在你的情况下:

guard let value = activityData.distance?.doubleValue else { return }
let measurement = Measurement(value: value, unit: UnitLength.meters).converted(to: .kilometers)

let mf = MeasurementFormatter()
mf.unitOptions = .providedUnit
mf.unitStyle = .medium
mf.numberFormatter.maximumFractionDigits = 1
mf.string(from: measurement) // "0.1 km"

关于ios - Core Motion 将距离转换为 KM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53580747/

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