Skip to content

Commit

Permalink
Fix decimal round (#36)
Browse files Browse the repository at this point in the history
* Remove NSDecimal usage

ref: https://developer.apple.com/documentation/xcode-release-notes/xcode-16-release-notes#Foundation

* Fix Date tests

* Fix date format tests

* Fix locale extension for tvOS

* Add more platform checks
  • Loading branch information
adrianrl authored Sep 26, 2024
1 parent 0105937 commit 6142942
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
16 changes: 6 additions & 10 deletions Source/Foundation/Extensions/DecimalExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,17 @@ public extension Decimal {

/// Return integer part
var integerPart: Int {
var result = Decimal()
var mutableSelf = self
NSDecimalRound(&result, &mutableSelf, 0, self >= 0 ? .down : .up)
return Int(truncating: NSDecimalNumber(decimal: result))
(self as NSNumber).intValue
}

/// Return decimal part
func decimalPart(decimals: Int) -> Int {
var result = Decimal()
let powered = pow(Decimal(10), decimals)
let integerPartToRemove = (powered * Decimal(abs(integerPart)))
var elevated = powered * abs(self)
let fractionalPart = self - Decimal((integerPart))
let multiplier = pow(10, decimals)
let scaledNumber = fractionalPart * multiplier
let truncatedNumber = Int(truncating: scaledNumber as NSNumber)

NSDecimalRound(&result, &elevated, 0, .down)
return Int(truncating: NSDecimalNumber(decimal: result - integerPartToRemove))
return abs(truncatedNumber)
}

/// Split the number into decimal and integer part
Expand Down
2 changes: 1 addition & 1 deletion Source/Foundation/Extensions/LocaleExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public extension Locale {

/// Fixed locale without the region
var fixed: Locale {
if #available(iOSApplicationExtension 16, *), #available(macOSApplicationExtension 13, *) {
if #available(iOSApplicationExtension 16, *), #available(macOSApplicationExtension 13, *), #available(tvOSApplicationExtension 16, *) {
Locale(languageCode: language.languageCode,
script: nil,
languageRegion: nil)
Expand Down
6 changes: 5 additions & 1 deletion Tests/Foundation/Extensions/DateExtensionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,11 @@ class DateExtensionsTests: XCTestCase {
XCTAssertEqual(date.formatted(with: .dateStyleMedium, locale: .spanishSpain, timeZone: .europeMadrid), "9 ago 2019")
XCTAssertEqual(date.formatted(with: .dateStyleFull, locale: .spanishSpain, timeZone: .europeMadrid), "viernes, 9 de agosto de 2019")
XCTAssertEqual(date.formatted(with: .dateStyleLong, locale: .spanishSpain, timeZone: .europeMadrid), "9 de agosto de 2019")
XCTAssertEqual(date.formatted(with: .dateStyleLong, locale: .catalanSpain, timeZone: .europeMadrid), "9 d’agost de 2019")
if #available(iOS 18.0, *), #available(macOS 15.0, *), #available(tvOS 18.0, *), #available(watchOS 11.0, *) {
XCTAssertEqual(date.formatted(with: .dateStyleLong, locale: .catalanSpain, timeZone: .europeMadrid), "9 d’agost del 2019")
} else {
XCTAssertEqual(date.formatted(with: .dateStyleLong, locale: .catalanSpain, timeZone: .europeMadrid), "9 d’agost de 2019")
}
XCTAssertTrue(date.formatted(with: .dateStyleLong, locale: .basqueSpain, timeZone: .europeMadrid).starts(with: "2019(e)ko abuztua"))
}

Expand Down
16 changes: 16 additions & 0 deletions Tests/Foundation/Extensions/DecimalExtensionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,22 @@ class DecimalExtensionsTests: XCTestCase {
XCTAssertEqual(result.decimalPart, 697)
}

func test_integer_part() {
XCTAssertEqual(Decimal(0).integerPart, 0)
XCTAssertEqual(Decimal(0.3).integerPart, 0)
XCTAssertEqual(Decimal(0.8).integerPart, 0)
XCTAssertEqual(Decimal(2.4).integerPart, 2)
XCTAssertEqual(Decimal(-3.8).integerPart, -3)
}

func test_decimal_part() {
XCTAssertEqual(Decimal(0).decimalPart(decimals: 0), 0)
XCTAssertEqual(Decimal(0.3).decimalPart(decimals: 1), 3)
XCTAssertEqual(Decimal(0.8).decimalPart(decimals: 2), 80)
XCTAssertEqual(Decimal(2.4).decimalPart(decimals: 0), 0)
XCTAssertEqual(Decimal(-3.8).decimalPart(decimals: 3), 800)
}

func test_format_decimal_part() {
XCTAssertEqual((5.532 as Decimal).formattedDecimalPart(decimals: 1, locale: .spanishSpain), ",5")
XCTAssertEqual((-5.532 as Decimal).formattedDecimalPart(decimals: 2, locale: .spanishSpain), ",53")
Expand Down

0 comments on commit 6142942

Please sign in to comment.