diff --git a/Sources/OTCore/Extensions/Foundation/Decimal.swift b/Sources/OTCore/Extensions/Foundation/Decimal.swift index a285260..8c1700a 100644 --- a/Sources/OTCore/Extensions/Foundation/Decimal.swift +++ b/Sources/OTCore/Extensions/Foundation/Decimal.swift @@ -252,6 +252,17 @@ extension Decimal { extension Decimal { + /// **OTCore:** + /// Similar to `Double.truncatingRemainder(dividingBy:)` from the standard Swift library. + public func truncatingRemainder(dividingBy rhs: Self) -> Self { + + let calculation = self / rhs + let integral = calculation.truncated(decimalPlaces: 0) + let fraction = self - (integral * rhs) + return fraction + + } + /// **OTCore:** /// Similar to `Int.quotientAndRemainder(dividingBy:)` from the standard Swift library. public func quotientAndRemainder(dividingBy rhs: Self) -> (quotient: Self, remainder: Self) { diff --git a/Tests/OTCoreTests/Extensions/Foundation/Decimal Tests.swift b/Tests/OTCoreTests/Extensions/Foundation/Decimal Tests.swift index d35cde2..9ba864d 100644 --- a/Tests/OTCoreTests/Extensions/Foundation/Decimal Tests.swift +++ b/Tests/OTCoreTests/Extensions/Foundation/Decimal Tests.swift @@ -126,6 +126,14 @@ class Extensions_Foundation_Decimal_Tests: XCTestCase { } + func testTruncatingRemainder() { + + let tr = Decimal(string: "20.5")!.truncatingRemainder(dividingBy: Decimal(8)) + + XCTAssertEqual(tr, Decimal(string: "4.5")!) + + } + func testQuotientAndRemainder() { let qr = Decimal(string: "17.5")!.quotientAndRemainder(dividingBy: 5.0)