Skip to content

Instantly share code, notes, and snippets.

@florianreinhart
Last active May 29, 2022 15:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save florianreinhart/99bb3a079075eaf640f1 to your computer and use it in GitHub Desktop.
Save florianreinhart/99bb3a079075eaf640f1 to your computer and use it in GitHub Desktop.
import Foundation
extension NSCalculationError: ErrorType {
}
extension NSDecimal: Comparable {
}
public func ==(lhs: NSDecimal, rhs: NSDecimal) -> Bool {
var mutableLhs = lhs
var mutableRhs = rhs
return (NSDecimalCompare(&mutableLhs, &mutableRhs) == .OrderedSame)
}
public func <(lhs: NSDecimal, rhs: NSDecimal) -> Bool {
var mutableLhs = lhs
var mutableRhs = rhs
return (NSDecimalCompare(&mutableLhs, &mutableRhs) == .OrderedAscending)
}
extension NSDecimal {
public var isNotANumber: Bool {
var mutableSelf = self
return NSDecimalIsNotANumber(&mutableSelf)
}
public mutating func compact() {
NSDecimalCompact(&self)
}
public mutating func round(scale: Int = Int(NSDecimalNoScale), roundingMode: NSRoundingMode = .RoundPlain) {
NSDecimalRound(&self, &self, scale, roundingMode)
}
public static func normalize(inout decimal1: NSDecimal, inout decimal2: NSDecimal, roundingMode: NSRoundingMode = .RoundPlain) throws {
let error = NSDecimalNormalize(&decimal1, &decimal2, roundingMode)
guard error == .NoError else {
throw error
}
}
public mutating func add(otherDecimal: NSDecimal, roundingMode: NSRoundingMode = .RoundPlain) throws {
var mutableOtherDecimal = otherDecimal
let error = NSDecimalAdd(&self, &self, &mutableOtherDecimal, roundingMode)
guard error == .NoError else {
throw error
}
}
public mutating func subtract(otherDecimal: NSDecimal, roundingMode: NSRoundingMode = .RoundPlain) throws {
var mutableOtherDecimal = otherDecimal
let error = NSDecimalSubtract(&self, &self, &mutableOtherDecimal, roundingMode)
guard error == .NoError else {
throw error
}
}
public mutating func multiplyBy(otherDecimal: NSDecimal, roundingMode: NSRoundingMode = .RoundPlain) throws {
var mutableOtherDecimal = otherDecimal
let error = NSDecimalMultiply(&self, &self, &mutableOtherDecimal, roundingMode)
guard error == .NoError else {
throw error
}
}
public mutating func divideBy(otherDecimal: NSDecimal, roundingMode: NSRoundingMode = .RoundPlain) throws {
var mutableOtherDecimal = otherDecimal
let error = NSDecimalDivide(&self, &self, &mutableOtherDecimal, roundingMode)
guard error == .NoError else {
throw error
}
}
public mutating func power(power: Int, roundingMode: NSRoundingMode = .RoundPlain) throws {
let error = NSDecimalPower(&self, &self, power, roundingMode)
guard error == .NoError else {
throw error
}
}
public mutating func multiplyByPowerOf10(power: Int16, roundingMode: NSRoundingMode = .RoundPlain) throws {
let error = NSDecimalMultiplyByPowerOf10(&self, &self, power, roundingMode)
guard error == .NoError else {
throw error
}
}
public func stringValue(locale: AnyObject? = nil) -> String {
var mutableSelf = self
return NSDecimalString(&mutableSelf, locale)
}
}
public func +(lhs: NSDecimal, rhs: NSDecimal) throws -> NSDecimal {
var result = lhs
try result.add(rhs)
return result
}
public func +=(inout lhs: NSDecimal, rhs: NSDecimal) throws {
try lhs.add(rhs)
}
public func -(lhs: NSDecimal, rhs: NSDecimal) throws -> NSDecimal {
var result = lhs
try result.subtract(rhs)
return result
}
public func -=(inout lhs: NSDecimal, rhs: NSDecimal) throws {
try lhs.subtract(rhs)
}
public func *(lhs: NSDecimal, rhs: NSDecimal) throws -> NSDecimal {
var result = lhs
try result.multiplyBy(rhs)
return result
}
public func *=(inout lhs: NSDecimal, rhs: NSDecimal) throws {
try lhs.multiplyBy(rhs)
}
public func /(lhs: NSDecimal, rhs: NSDecimal) throws -> NSDecimal {
var result = lhs
try result.divideBy(rhs)
return result
}
public func /=(inout lhs: NSDecimal, rhs: NSDecimal) throws {
try lhs.divideBy(rhs)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment