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

前端 未结 28 3272
日久生厌
日久生厌 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条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 06:37

    I would use

    print(String(format: "%.3f", totalWorkTimeInHours))
    

    and change .3f to any number of decimal numbers you need

提交回复
热议问题