diff --git a/Source/Foundation/Extensions/DecimalExtensions.swift b/Source/Foundation/Extensions/DecimalExtensions.swift index b7d2645..4a98667 100644 --- a/Source/Foundation/Extensions/DecimalExtensions.swift +++ b/Source/Foundation/Extensions/DecimalExtensions.swift @@ -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 diff --git a/Source/Foundation/Extensions/LocaleExtensions.swift b/Source/Foundation/Extensions/LocaleExtensions.swift index 129afa3..4fa58ec 100644 --- a/Source/Foundation/Extensions/LocaleExtensions.swift +++ b/Source/Foundation/Extensions/LocaleExtensions.swift @@ -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) diff --git a/Tests/Foundation/Extensions/DateExtensionsTests.swift b/Tests/Foundation/Extensions/DateExtensionsTests.swift index 4da9a7e..54d85af 100644 --- a/Tests/Foundation/Extensions/DateExtensionsTests.swift +++ b/Tests/Foundation/Extensions/DateExtensionsTests.swift @@ -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")) } diff --git a/Tests/Foundation/Extensions/DecimalExtensionsTests.swift b/Tests/Foundation/Extensions/DecimalExtensionsTests.swift index e4d9709..b2c8607 100644 --- a/Tests/Foundation/Extensions/DecimalExtensionsTests.swift +++ b/Tests/Foundation/Extensions/DecimalExtensionsTests.swift @@ -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")