在swift中將雙精度值四捨五入到x個小數位數 - Rounding a double value to x number of decimal places in swift

問題:

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章