Rounding a double value to x number of decimal places in swift

前端 未结 28 3227
日久生厌
日久生厌 2020-11-22 06:11

Can anyone tell me how to round a double value to x number of decimal places in Swift?

I have:

var totalWorkTimeInHours = (totalWorkTime/60/60)
         


        
28条回答
  •  無奈伤痛
    2020-11-22 06:36

    //find the distance between two points
    let coordinateSource = CLLocation(latitude: 30.7717625, longitude:76.5741449 )
    let coordinateDestination = CLLocation(latitude: 29.9810859, longitude: 76.5663599)
    let distanceInMeters = coordinateSource.distance(from: coordinateDestination)
    let valueInKms = distanceInMeters/1000
    let preciseValueUptoThreeDigit = Double(round(1000*valueInKms)/1000)
    self.lblTotalDistance.text = "Distance is : \(preciseValueUptoThreeDigit) kms"
    

提交回复
热议问题