如何附加海拔整数

How do I append Altitude Integers

我需要在我正在构建的当前应用程序中显示当前海拔高度。但是,当确定海拔高度时,标签会显示 xxx.xxxxxxxxxxxx 它在小数点的另一边走得很远。我只希望它在小数点的另一边最多给出 2 个数字。这怎么可能?

    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.requestWhenInUseAuthorization()
    manager.startUpdatingLocation()

     func locationManager(_ manager: CLLocationManager,            didUpdateLocations locations: [CLLocation]) {

    let location = locations [0]

    self.speedLabel.text = String(location.speed)


    self.altitudeLabel.text = String(location.altitude)
    CLGeocoder().reverseGeocodeLocation(location) { (placemarks, error) in

        if error != nil {
            print(error!)

        }

这是我当前的代码。

正如我所说,我只想将高度显示为 "XX.XX" 而不是 XX.XXXXXXXXX

试一试:

self.altitudeLabel.text = String(format: "%.2f", location.altitude)

试试这个:

extension Double {
    func decimal(places: Int) -> Double {
        let resultString = NSString(format: "%.\(places)f" as NSString, self) as String
        return Double(resultString)!
    }
}

self.altitudeLabel.text = location.altitude.decimal(位置:2)