Skip to content

Instantly share code, notes, and snippets.

@descorp
Created October 24, 2018 13:10
Show Gist options
  • Save descorp/dcc31691d7a3bfd0cc4dc6ac9f7e587a to your computer and use it in GitHub Desktop.
Save descorp/dcc31691d7a3bfd0cc4dc6ac9f7e587a to your computer and use it in GitHub Desktop.
Float Precision
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