Skip to content

Commit

Permalink
Merge pull request #10 from orchetect/dev
Browse files Browse the repository at this point in the history
Added `Decimal.truncatingRemainder(dividingBy:)`
  • Loading branch information
orchetect authored Jul 31, 2021
2 parents f67728f + d4ad41f commit 682f6c4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Sources/OTCore/Extensions/Foundation/Decimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions Tests/OTCoreTests/Extensions/Foundation/Decimal Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 682f6c4

Please sign in to comment.