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

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

    var n = 123.111222333
    n = Double(Int(n * 10.0)) / 10.0
    

    Result: n = 123.1

    Change 10.0 (1 decimal place) to any of 100.0 (2 decimal place), 1000.0 (3 decimal place) and so on, for the number of digits you want after decimal..

提交回复
热议问题