問題:
Can anyone tell me how to round a double value to x number of decimal places in Swift?誰能告訴我如何在 Swift 中將 double 值四捨五入到 x 位小數?
I have:我有:
var totalWorkTimeInHours = (totalWorkTime/60/60)
With totalWorkTime
being an NSTimeInterval (double) in second. totalWorkTime
是 NSTimeInterval (double) 秒。
totalWorkTimeInHours
will give me the hours, but it gives me the amount of time in such a long precise number eg 1.543240952039...... totalWorkTimeInHours
會給我小時數,但它給了我這麼長的精確數字的時間量,例如 1.543240952039 ......
How do I round this down to, say, 1.543 when I print totalWorkTimeInHours
?當我打印totalWorkTimeInHours
時,如何將其舍入到 1.543 ?
解決方案:
參考一: https://en.stackoom.com/question/1qi0j參考二: https://stackoom.com/question/1qi0j