# “％is unavailable：使用truncatingRemainder”是什么意思？

``extension CMTime { var durationText:String { let totalSeconds = CMTimeGetSeconds(self) let hours:Int = Int(totalSeconds / 3600) let minutes:Int = Int(totalSeconds % 3600 / 60) let seconds:Int = Int(totalSeconds % 60) if hours > 0 { return String(format: "%i:%02i:%02i", hours, minutes, seconds) } else { return String(format: "%02i:%02i", minutes, seconds) } } }` `

`CMTimeGetSeconds()`返回一个浮点数（ `Float64` aka `Double` ）。 在Swift 2中，你可以计算浮点除法的其余部分

` `let rem = 2.5 % 1.1 print(rem) // 0.3` `

` `let rem = 2.5.truncatingRemainder(dividingBy: 1.1) print(rem) // 0.3` `

` `let totalSeconds = CMTimeGetSeconds(self) let hours = Int(totalSeconds / 3600) let minutes = Int((totalSeconds.truncatingRemainder(dividingBy: 3600)) / 60) let seconds = Int(totalSeconds.truncatingRemainder(dividingBy: 60))` `

` `let totalSeconds = Int(CMTimeGetSeconds(self)) // Truncate to integer // Or: let totalSeconds = lrint(CMTimeGetSeconds(self)) // Round to nearest integer` `

` `let hours = totalSeconds / 3600 let minutes = (totalSeconds % 3600) / 60 let seconds = totalSeconds % 60` `

`%`模数运算符仅针对整数types定义。 对于浮点types，您需要更具体地了解所需的IEEE 754除法/余数行为，因此您必须调用一个方法： `remainder``truncatingRemainder` 。 （如果你正在做浮点math，你实际上需要关心这个，还有很多其他的东西 ，否则你会得到意想不到的结果。

### 在swift 3中带回简单的模态语法：

` `infix operator %%/*<--infix operator is required for custom infix char combos*/ /** * Brings back simple modulo syntax (was removed in swift 3) * Calculates the remainder of expression1 divided by expression2 * The sign of the modulo result matches the sign of the dividend (the first number). For example, -4 % 3 and -4 % -3 both evaluate to -1 * EXAMPLE: * print(12 %% 5) // 2 * print(4.3 %% 2.1) // 0.0999999999999996 * print(4 %% 4) // 0 * NOTE: The first print returns 2, rather than 12/5 or 2.4, because the modulo (%) operator returns only the remainder. The second trace returns 0.0999999999999996 instead of the expected 0.1 because of the limitations of floating-point accuracy in binary computing. * NOTE: Int's can still use single % * NOTE: there is also .remainder which supports returning negatives as oppose to truncatingRemainder (aka the old %) which returns only positive. */ public func %% (left:CGFloat, right:CGFloat) -> CGFloat { return left.truncatingRemainder(dividingBy: right) }` `

` ` let minutes = Int(floor(totalSeconds / 60)) let seconds = Int(totalSeconds) % 60` `

`totalSeconds``TimeInterval``Double` ）。