3 回答

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超3個(gè)贊
round
Double
let x = 1.23556789let y = Double(round(1000*x)/1000)print(y) // 1.236
printf(...)
String(format: ...)
Double
.
編輯:

TA貢獻(xiàn)1934條經(jīng)驗(yàn) 獲得超2個(gè)贊
SWIFT 2分機(jī)
extension Double { /// Rounds the double to decimal places value func roundToPlaces(places:Int) -> Double { let divisor = pow(10.0, Double(places)) return round(self * divisor) / divisor }}
SWIFT 3分機(jī)
round
rounded
:
extension Double { /// Rounds the double to decimal places value func rounded(toPlaces places:Int) -> Double { let divisor = pow(10.0, Double(places)) return (self * divisor).rounded() / divisor }}
let x = Double(0.123456789).roundToPlaces(4) // x becomes 0.1235 under Swift 2 let x = Double(0.123456789).rounded(toPlaces: 4) // Swift 3 version

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊
我怎樣才能把這個(gè)算到,比方說(shuō),1.543呢? 當(dāng)我打印totalWorkTimeInHours
?
totalWorkTimeInHours
String
format
print(String(format: "%.3f", totalWorkTimeInHours))
- 3 回答
- 0 關(guān)注
- 621 瀏覽
添加回答
舉報(bào)