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

前端 未结 28 3072
日久生厌
日久生厌 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

    In Swift 5.3 and Xcode 12:

    let pi: Double = 3.14159265358979
    String(format:"%.2f", pi)
    

    Example:

    PS.: It still the same since Swift 2.0 and Xcode 7.2

提交回复
热议问题