Created
October 24, 2018 13:10
-
-
Save descorp/dcc31691d7a3bfd0cc4dc6ac9f7e587a to your computer and use it in GitHub Desktop.
Float Precision
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Decimal { | |
init(value: NSNumber, precision: Int) { | |
let exponent = pow(10.0, Double(precision)) | |
let mantisa = Decimal((value.doubleValue * exponent).rounded()) | |
let sign = value.floatValue > 0 ? FloatingPointSign.plus : FloatingPointSign.minus | |
self = Decimal(sign: sign, exponent: -precision, significand: mantisa) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment