I want to add a round method with the specific rounding digits to my RealmOptional class. If you want to add the method to your CGFloat class, you can write the following (I borrow it from this answer):
public extension CGFloat {
func roundToDecimals(decimals: Int = 2) -> CGFloat {
let multiplier = CGFloat(10^decimals)
return round(multiplier * self) / multiplier
}
}
Is it possible to add this functionality to the RealmOptional class, where only type defined as Float must be added the method to.
I can't get how it shall be implemented, but the following spit out the error:
Type 'T' constrained to non-protocol type 'Float'
import RealmSwift
extension RealmOptional where T: Float {
func roundToDecimals(decimals: Int = 2) -> CGFloat {
let multiplier = CGFloat(10^decimals)
return round(multiplier * self.value!) / multiplier
}
}