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

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

    How do I round this down to, say, 1.543 when I print totalWorkTimeInHours?

    To round totalWorkTimeInHours to 3 digits for printing, use the String constructor which takes a format string:

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

提交回复
热议问题