From 278aa60a8c33739786f2552d0124edd24bb3ebf7 Mon Sep 17 00:00:00 2001 From: Maximilian Litteral Date: Tue, 19 Feb 2019 16:13:28 -0500 Subject: [PATCH 1/2] Replace ip_round extension on Double for BinaryFloatingPoint --- SwiftWisdom.xcodeproj/project.pbxproj | 48 ++++--------------- .../BinaryFloatingPoint+Extensions.swift | 18 +++++++ .../Numbers/Double+Extensions.swift | 18 ------- ...BinaryFloatingPoint+ExtensionsTests.swift} | 6 +-- 4 files changed, 29 insertions(+), 61 deletions(-) create mode 100644 SwiftWisdom/Core/StandardLibrary/Numbers/BinaryFloatingPoint+Extensions.swift delete mode 100644 SwiftWisdom/Core/StandardLibrary/Numbers/Double+Extensions.swift rename SwiftWisdomTests/StandardLibrary/Numbers/{Double+ExtensionsTests.swift => BinaryFloatingPoint+ExtensionsTests.swift} (91%) diff --git a/SwiftWisdom.xcodeproj/project.pbxproj b/SwiftWisdom.xcodeproj/project.pbxproj index a5f26ee..fd0d639 100644 --- a/SwiftWisdom.xcodeproj/project.pbxproj +++ b/SwiftWisdom.xcodeproj/project.pbxproj @@ -8,7 +8,7 @@ /* Begin PBXBuildFile section */ 0922A8531FAD0A78005AD966 /* UIView+SafeArea.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0922A8521FAD0A78005AD966 /* UIView+SafeArea.swift */; }; - 0CED7D671CD6971400672913 /* Double+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CED7D661CD6971400672913 /* Double+Extensions.swift */; }; + 0CED7D671CD6971400672913 /* BinaryFloatingPoint+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CED7D661CD6971400672913 /* BinaryFloatingPoint+Extensions.swift */; }; 13425F701DB073F300F446BE /* Sequence+UtilitiesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13425F631DB073F300F446BE /* Sequence+UtilitiesTests.swift */; }; 13425F741DB073F300F446BE /* MathableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13425F691DB073F300F446BE /* MathableTests.swift */; }; 13425F751DB073F300F446BE /* RawRepresentableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13425F6B1DB073F300F446BE /* RawRepresentableTests.swift */; }; @@ -17,7 +17,7 @@ 13425F7C1DB0755B00F446BE /* NSMutableAttributedString+Format.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13425F7B1DB0755B00F446BE /* NSMutableAttributedString+Format.swift */; }; 13425F7E1DB0757000F446BE /* NSMutableAttributedString+FormatTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13425F7D1DB0757000F446BE /* NSMutableAttributedString+FormatTests.swift */; }; 13425F811DB075A000F446BE /* Integer+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13425F801DB075A000F446BE /* Integer+Extensions.swift */; }; - 13425F851DB075B300F446BE /* Double+ExtensionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13425F821DB075B300F446BE /* Double+ExtensionsTests.swift */; }; + 13425F851DB075B300F446BE /* BinaryFloatingPoint+ExtensionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13425F821DB075B300F446BE /* BinaryFloatingPoint+ExtensionsTests.swift */; }; 13425F861DB075B300F446BE /* Integer+ExtensionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13425F831DB075B300F446BE /* Integer+ExtensionsTests.swift */; }; 13425F871DB075B300F446BE /* UnsignedInteger+ExtensionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13425F841DB075B300F446BE /* UnsignedInteger+ExtensionsTests.swift */; }; 13425F891DB075E000F446BE /* String+Indexing.swift in Sources */ = {isa = PBXBuildFile; fileRef = 13425F881DB075E000F446BE /* String+Indexing.swift */; }; @@ -131,7 +131,7 @@ /* Begin PBXFileReference section */ 0922A8521FAD0A78005AD966 /* UIView+SafeArea.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIView+SafeArea.swift"; sourceTree = ""; }; - 0CED7D661CD6971400672913 /* Double+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Double+Extensions.swift"; sourceTree = ""; }; + 0CED7D661CD6971400672913 /* BinaryFloatingPoint+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "BinaryFloatingPoint+Extensions.swift"; sourceTree = ""; }; 13425F631DB073F300F446BE /* Sequence+UtilitiesTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Sequence+UtilitiesTests.swift"; sourceTree = ""; }; 13425F691DB073F300F446BE /* MathableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MathableTests.swift; sourceTree = ""; }; 13425F6B1DB073F300F446BE /* RawRepresentableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RawRepresentableTests.swift; sourceTree = ""; }; @@ -140,7 +140,7 @@ 13425F7B1DB0755B00F446BE /* NSMutableAttributedString+Format.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSMutableAttributedString+Format.swift"; sourceTree = ""; }; 13425F7D1DB0757000F446BE /* NSMutableAttributedString+FormatTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "NSMutableAttributedString+FormatTests.swift"; path = "NSAttributedString/NSMutableAttributedString+FormatTests.swift"; sourceTree = ""; }; 13425F801DB075A000F446BE /* Integer+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Integer+Extensions.swift"; sourceTree = ""; }; - 13425F821DB075B300F446BE /* Double+ExtensionsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Double+ExtensionsTests.swift"; sourceTree = ""; }; + 13425F821DB075B300F446BE /* BinaryFloatingPoint+ExtensionsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "BinaryFloatingPoint+ExtensionsTests.swift"; sourceTree = ""; }; 13425F831DB075B300F446BE /* Integer+ExtensionsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Integer+ExtensionsTests.swift"; sourceTree = ""; }; 13425F841DB075B300F446BE /* UnsignedInteger+ExtensionsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UnsignedInteger+ExtensionsTests.swift"; sourceTree = ""; }; 13425F881DB075E000F446BE /* String+Indexing.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+Indexing.swift"; sourceTree = ""; }; @@ -306,7 +306,7 @@ 13425F651DB073F300F446BE /* Numbers */ = { isa = PBXGroup; children = ( - 13425F821DB075B300F446BE /* Double+ExtensionsTests.swift */, + 13425F821DB075B300F446BE /* BinaryFloatingPoint+ExtensionsTests.swift */, 13425F831DB075B300F446BE /* Integer+ExtensionsTests.swift */, 13425F841DB075B300F446BE /* UnsignedInteger+ExtensionsTests.swift */, 13425F691DB073F300F446BE /* MathableTests.swift */, @@ -737,7 +737,7 @@ 13425F801DB075A000F446BE /* Integer+Extensions.swift */, 80D63D2C1C4F251E00C88D00 /* UnsignedInteger+Extensions.swift */, 1CE3D0671C6E61A500FB676C /* Mathable.swift */, - 0CED7D661CD6971400672913 /* Double+Extensions.swift */, + 0CED7D661CD6971400672913 /* BinaryFloatingPoint+Extensions.swift */, ); path = Numbers; sourceTree = ""; @@ -809,7 +809,6 @@ 80232B9C1BF2EE2E00818B6E /* Frameworks */, 80232B9D1BF2EE2E00818B6E /* Resources */, BEDBF20DB05D76F60C858449 /* [CP] Embed Pods Frameworks */, - E3F5EA111B900D6479C8D173 /* [CP] Copy Pods Resources */, 3169F10A1F06850F00C917B2 /* SwiftLint */, ); buildRules = ( @@ -830,7 +829,6 @@ 80232BB01BF2EE2E00818B6E /* Frameworks */, 80232BB11BF2EE2E00818B6E /* Resources */, FAA5EC81369AE73A0E00ED4B /* [CP] Embed Pods Frameworks */, - 77882022D65A5916A7FFD999 /* [CP] Copy Pods Resources */, ); buildRules = ( ); @@ -938,21 +936,6 @@ shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n"; showEnvVarsInLog = 0; }; - 77882022D65A5916A7FFD999 /* [CP] Copy Pods Resources */ = { - isa = PBXShellScriptBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - inputPaths = ( - ); - name = "[CP] Copy Pods Resources"; - outputPaths = ( - ); - runOnlyForDeploymentPostprocessing = 0; - shellPath = /bin/sh; - shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-SwiftWisdomTests/Pods-SwiftWisdomTests-resources.sh\"\n"; - showEnvVarsInLog = 0; - }; BEDBF20DB05D76F60C858449 /* [CP] Embed Pods Frameworks */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; @@ -993,21 +976,6 @@ shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n"; showEnvVarsInLog = 0; }; - E3F5EA111B900D6479C8D173 /* [CP] Copy Pods Resources */ = { - isa = PBXShellScriptBuildPhase; - buildActionMask = 2147483647; - files = ( - ); - inputPaths = ( - ); - name = "[CP] Copy Pods Resources"; - outputPaths = ( - ); - runOnlyForDeploymentPostprocessing = 0; - shellPath = /bin/sh; - shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-SwiftWisdom/Pods-SwiftWisdom-resources.sh\"\n"; - showEnvVarsInLog = 0; - }; FAA5EC81369AE73A0E00ED4B /* [CP] Embed Pods Frameworks */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; @@ -1091,7 +1059,7 @@ 80232C061BF2F1BD00818B6E /* Set+Utilities.swift in Sources */, 8002059E1BF683E0005852C9 /* Result.swift in Sources */, 800205A21BF684C7005852C9 /* NSDate+Comparable.swift in Sources */, - 0CED7D671CD6971400672913 /* Double+Extensions.swift in Sources */, + 0CED7D671CD6971400672913 /* BinaryFloatingPoint+Extensions.swift in Sources */, 80D63D2F1C4F251E00C88D00 /* UnsignedInteger+Extensions.swift in Sources */, 80232C0F1BF2FB3800818B6E /* ViewController.swift in Sources */, 1CB92B7C1EC65DAE007EBD1E /* RepeatedTask.swift in Sources */, @@ -1123,7 +1091,7 @@ 1C16E3091D2809C5008A1001 /* ComparablesTests.swift in Sources */, FF44B77B1CDCD19A00764AB0 /* Rx+ExtensionsTest.swift in Sources */, 13425F8F1DB0761500F446BE /* String+EmptyTests.swift in Sources */, - 13425F851DB075B300F446BE /* Double+ExtensionsTests.swift in Sources */, + 13425F851DB075B300F446BE /* BinaryFloatingPoint+ExtensionsTests.swift in Sources */, 80D63D221C4F22B800C88D00 /* BitMaskTests.swift in Sources */, CDCDE25D1E67D785007B3784 /* TimeOfDayTests.swift in Sources */, E7A6139A1E3FB521006382DA /* Rx+RepeatingTimeoutsTests.swift in Sources */, diff --git a/SwiftWisdom/Core/StandardLibrary/Numbers/BinaryFloatingPoint+Extensions.swift b/SwiftWisdom/Core/StandardLibrary/Numbers/BinaryFloatingPoint+Extensions.swift new file mode 100644 index 0000000..5fe5f52 --- /dev/null +++ b/SwiftWisdom/Core/StandardLibrary/Numbers/BinaryFloatingPoint+Extensions.swift @@ -0,0 +1,18 @@ +// +// BinaryFloatingPoint+Extensions.swift +// SwiftWisdom +// +// Created by Stephen Wong on 5/1/16. +// Copyright © 2016 Intrepid. All rights reserved. +// + +import Foundation + +extension BinaryFloatingPoint { + // https://codereview.stackexchange.com/questions/142748/swift-floatingpoint-rounded-to-places + public func ip_round(toDecimalPlaces places: Int) -> Self { + guard places >= 0 else { return self } + let divisor = Self((0.. Double { - let decimalShiftBase10 = pow(10.0, Double(decimalPlaces)) - let roundedNumber = (self * decimalShiftBase10).rounded() - let shiftDecimalPlaces = roundedNumber / decimalShiftBase10 - return shiftDecimalPlaces - } -} diff --git a/SwiftWisdomTests/StandardLibrary/Numbers/Double+ExtensionsTests.swift b/SwiftWisdomTests/StandardLibrary/Numbers/BinaryFloatingPoint+ExtensionsTests.swift similarity index 91% rename from SwiftWisdomTests/StandardLibrary/Numbers/Double+ExtensionsTests.swift rename to SwiftWisdomTests/StandardLibrary/Numbers/BinaryFloatingPoint+ExtensionsTests.swift index a72073e..365867e 100644 --- a/SwiftWisdomTests/StandardLibrary/Numbers/Double+ExtensionsTests.swift +++ b/SwiftWisdomTests/StandardLibrary/Numbers/BinaryFloatingPoint+ExtensionsTests.swift @@ -1,5 +1,5 @@ // -// DoubleTests.swift +// BinaryFloatingPointTests.swift // SwiftWisdom // // Created by Stephen Wong on 5/1/16. @@ -9,14 +9,14 @@ import XCTest import SwiftWisdom -class DoubleTests: XCTestCase { +class BinaryFloatingPointTests: XCTestCase { func testRounding() { let unroundedDoubles = [.pi, 5.0156, 10.014, 9999.99499] let roundedToOneDecimalPlaces = [3.1, 5.0, 10.0, 10000.0] let roundedToTwoDecimalPlaces = [3.14, 5.02, 10.01, 9999.99] let roundedToFiveDecimalPlaces = [3.14159, 5.0156, 10.014, 9999.99499] - + for (index, unroundedDouble) in unroundedDoubles.enumerated() { XCTAssertEqual(unroundedDouble.ip_round(toDecimalPlaces: 1), roundedToOneDecimalPlaces[index]) XCTAssertEqual(unroundedDouble.ip_round(toDecimalPlaces: 2), roundedToTwoDecimalPlaces[index]) From 05daa70261097729c8000e3e63b761cb1ac02f65 Mon Sep 17 00:00:00 2001 From: Maximilian Litteral Date: Fri, 22 Feb 2019 10:31:18 -0500 Subject: [PATCH 2/2] Address comments --- SwiftWisdom.xcodeproj/project.pbxproj | 4 ++ .../BinaryFloatingPoint+Extensions.swift | 11 ++- .../Numbers/Double+Extensions.swift | 19 +++++ .../Numbers/Integer+Extensions.swift | 2 +- .../BinaryFloatingPoint+ExtensionsTests.swift | 69 +++++++++++++++++-- 5 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 SwiftWisdom/Core/StandardLibrary/Numbers/Double+Extensions.swift diff --git a/SwiftWisdom.xcodeproj/project.pbxproj b/SwiftWisdom.xcodeproj/project.pbxproj index fd0d639..3068ad3 100644 --- a/SwiftWisdom.xcodeproj/project.pbxproj +++ b/SwiftWisdom.xcodeproj/project.pbxproj @@ -42,6 +42,7 @@ 1CE3D0681C6E61A500FB676C /* Mathable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CE3D0671C6E61A500FB676C /* Mathable.swift */; }; 1CFEBD051E8047D60078108A /* DateFormattingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CFEBD041E8047D60078108A /* DateFormattingTests.swift */; }; 1CFEBD081E8055AB0078108A /* UIButtonTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1CFEBD071E8055AB0078108A /* UIButtonTests.swift */; }; + 211E2D94222049D20073A9C8 /* Double+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 211E2D93222049D20073A9C8 /* Double+Extensions.swift */; }; 311178591E16CACF00294C25 /* String+Attributed.swift in Sources */ = {isa = PBXBuildFile; fileRef = 311178581E16CACF00294C25 /* String+Attributed.swift */; }; 314F3BF61E9EA7E7005753BE /* VideoPlayerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 314F3BF51E9EA7E7005753BE /* VideoPlayerTests.swift */; }; 314F3BF91E9EA92B005753BE /* testvideoone.mp4 in Resources */ = {isa = PBXBuildFile; fileRef = 314F3BF71E9EA92B005753BE /* testvideoone.mp4 */; }; @@ -165,6 +166,7 @@ 1CE3D0671C6E61A500FB676C /* Mathable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Mathable.swift; sourceTree = ""; }; 1CFEBD041E8047D60078108A /* DateFormattingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = DateFormattingTests.swift; path = Date/DateFormattingTests.swift; sourceTree = ""; }; 1CFEBD071E8055AB0078108A /* UIButtonTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIButtonTests.swift; sourceTree = ""; }; + 211E2D93222049D20073A9C8 /* Double+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Double+Extensions.swift"; sourceTree = ""; }; 311178581E16CACF00294C25 /* String+Attributed.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+Attributed.swift"; sourceTree = ""; }; 314F3BF51E9EA7E7005753BE /* VideoPlayerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VideoPlayerTests.swift; sourceTree = ""; }; 314F3BF71E9EA92B005753BE /* testvideoone.mp4 */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = testvideoone.mp4; sourceTree = ""; }; @@ -737,6 +739,7 @@ 13425F801DB075A000F446BE /* Integer+Extensions.swift */, 80D63D2C1C4F251E00C88D00 /* UnsignedInteger+Extensions.swift */, 1CE3D0671C6E61A500FB676C /* Mathable.swift */, + 211E2D93222049D20073A9C8 /* Double+Extensions.swift */, 0CED7D661CD6971400672913 /* BinaryFloatingPoint+Extensions.swift */, ); path = Numbers; @@ -1021,6 +1024,7 @@ 80232C0D1BF2FAA100818B6E /* AppDelegate.swift in Sources */, 80232BFF1BF2F1BD00818B6E /* Qu.swift in Sources */, 805807DF1C5BEDE400F9AFB1 /* UIImage+ColorImage.swift in Sources */, + 211E2D94222049D20073A9C8 /* Double+Extensions.swift in Sources */, 31C0E33F1E15AA72009AB216 /* UIView+Extensions.swift in Sources */, 319CB0E51E14502F00BBD79B /* Date+Extensions.swift in Sources */, 802C4BBA1C1B6F7C00C69D80 /* Version.swift in Sources */, diff --git a/SwiftWisdom/Core/StandardLibrary/Numbers/BinaryFloatingPoint+Extensions.swift b/SwiftWisdom/Core/StandardLibrary/Numbers/BinaryFloatingPoint+Extensions.swift index 5fe5f52..86045be 100644 --- a/SwiftWisdom/Core/StandardLibrary/Numbers/BinaryFloatingPoint+Extensions.swift +++ b/SwiftWisdom/Core/StandardLibrary/Numbers/BinaryFloatingPoint+Extensions.swift @@ -9,10 +9,17 @@ import Foundation extension BinaryFloatingPoint { - // https://codereview.stackexchange.com/questions/142748/swift-floatingpoint-rounded-to-places - public func ip_round(toDecimalPlaces places: Int) -> Self { + /// https://codereview.stackexchange.com/questions/142748/swift-floatingpoint-rounded-to-places + public func ip_rounded(toDecimalPlaces places: Int) -> Self { guard places >= 0 else { return self } let divisor = Self((0..= 0 else { return } + let divisor = Self((0.. Double { + let decimalShiftBase10 = pow(10.0, Double(decimalPlaces)) + let roundedNumber = (self * decimalShiftBase10).rounded() + let shiftDecimalPlaces = roundedNumber / decimalShiftBase10 + return shiftDecimalPlaces + } +} diff --git a/SwiftWisdom/Core/StandardLibrary/Numbers/Integer+Extensions.swift b/SwiftWisdom/Core/StandardLibrary/Numbers/Integer+Extensions.swift index 389abd7..432a093 100644 --- a/SwiftWisdom/Core/StandardLibrary/Numbers/Integer+Extensions.swift +++ b/SwiftWisdom/Core/StandardLibrary/Numbers/Integer+Extensions.swift @@ -29,7 +29,7 @@ extension BinaryInteger { var magnitude: Int = Int(log10(value) / 3.0) // the order of magnitude of our value in thousands // divide value by 1000^magnitude to get hundreds value, then round to desired decimal places - var roundedHundredsValue = (value / pow(1000.0, Double(magnitude))).ip_round(toDecimalPlaces: decimalPlaces) + var roundedHundredsValue = (value / pow(1000.0, Double(magnitude))).ip_rounded(toDecimalPlaces: decimalPlaces) // if rounding brings our display value over 1000, divide by 1000 and then bump the magnitude if roundedHundredsValue >= 1000 { diff --git a/SwiftWisdomTests/StandardLibrary/Numbers/BinaryFloatingPoint+ExtensionsTests.swift b/SwiftWisdomTests/StandardLibrary/Numbers/BinaryFloatingPoint+ExtensionsTests.swift index 365867e..64fe451 100644 --- a/SwiftWisdomTests/StandardLibrary/Numbers/BinaryFloatingPoint+ExtensionsTests.swift +++ b/SwiftWisdomTests/StandardLibrary/Numbers/BinaryFloatingPoint+ExtensionsTests.swift @@ -11,16 +11,77 @@ import SwiftWisdom class BinaryFloatingPointTests: XCTestCase { - func testRounding() { + func testRoundingDoubles() { let unroundedDoubles = [.pi, 5.0156, 10.014, 9999.99499] let roundedToOneDecimalPlaces = [3.1, 5.0, 10.0, 10000.0] let roundedToTwoDecimalPlaces = [3.14, 5.02, 10.01, 9999.99] let roundedToFiveDecimalPlaces = [3.14159, 5.0156, 10.014, 9999.99499] for (index, unroundedDouble) in unroundedDoubles.enumerated() { - XCTAssertEqual(unroundedDouble.ip_round(toDecimalPlaces: 1), roundedToOneDecimalPlaces[index]) - XCTAssertEqual(unroundedDouble.ip_round(toDecimalPlaces: 2), roundedToTwoDecimalPlaces[index]) - XCTAssertEqual(unroundedDouble.ip_round(toDecimalPlaces: 5), roundedToFiveDecimalPlaces[index]) + XCTAssertEqual(unroundedDouble.ip_rounded(toDecimalPlaces: 1), roundedToOneDecimalPlaces[index]) + XCTAssertEqual(unroundedDouble.ip_rounded(toDecimalPlaces: 2), roundedToTwoDecimalPlaces[index]) + XCTAssertEqual(unroundedDouble.ip_rounded(toDecimalPlaces: 5), roundedToFiveDecimalPlaces[index]) } } + + func testRoundingFloats() { + let unroundedFloats: [Float] = [.pi, 5.0156, 10.014, 9999.99499] + let roundedToOneDecimalPlaces: [Float] = [3.1, 5.0, 10.0, 10000.0] + let roundedToTwoDecimalPlaces: [Float] = [3.14, 5.02, 10.01, 9999.99] + let roundedToFiveDecimalPlaces: [Float] = [3.14159, 5.0156, 10.014, 9999.99499] + + for (index, unroundedDouble) in unroundedFloats.enumerated() { + XCTAssertEqual(unroundedDouble.ip_rounded(toDecimalPlaces: 1), roundedToOneDecimalPlaces[index]) + XCTAssertEqual(unroundedDouble.ip_rounded(toDecimalPlaces: 2), roundedToTwoDecimalPlaces[index], accuracy: 0.01) + XCTAssertEqual(unroundedDouble.ip_rounded(toDecimalPlaces: 5), roundedToFiveDecimalPlaces[index]) + } + } + + func testRoundingCGFloats() { + let unroundedFloats: [CGFloat] = [.pi, 5.0156, 10.014, 9999.99499] + let roundedToOneDecimalPlaces: [CGFloat] = [3.1, 5.0, 10.0, 10000.0] + let roundedToTwoDecimalPlaces: [CGFloat] = [3.14, 5.02, 10.01, 9999.99] + let roundedToFiveDecimalPlaces: [CGFloat] = [3.14159, 5.0156, 10.014, 9999.99499] + + for (index, unroundedDouble) in unroundedFloats.enumerated() { + XCTAssertEqual(unroundedDouble.ip_rounded(toDecimalPlaces: 1), roundedToOneDecimalPlaces[index]) + XCTAssertEqual(unroundedDouble.ip_rounded(toDecimalPlaces: 2), roundedToTwoDecimalPlaces[index]) + XCTAssertEqual(unroundedDouble.ip_rounded(toDecimalPlaces: 5), roundedToFiveDecimalPlaces[index]) + } + } + + func testRoundInPlace() { + // Double + var pi_double: Double = .pi + pi_double.ip_round(toDecimalPlaces: 1) + XCTAssertEqual(pi_double, 3.1) + pi_double = .pi // Reset + pi_double.ip_round(toDecimalPlaces: 2) + XCTAssertEqual(pi_double, 3.14) + pi_double = .pi // Reset + pi_double.ip_round(toDecimalPlaces: 5) + XCTAssertEqual(pi_double, 3.14159) + + // Float + var pi_float: Float = .pi + pi_float.ip_round(toDecimalPlaces: 1) + XCTAssertEqual(pi_float, 3.1) + pi_float = .pi // Reset + pi_float.ip_round(toDecimalPlaces: 2) + XCTAssertEqual(pi_float, 3.14) + pi_float = .pi // Reset + pi_float.ip_round(toDecimalPlaces: 5) + XCTAssertEqual(pi_float, 3.14159) + + // CGFloat + var pi_cgfloat: CGFloat = .pi + pi_cgfloat.ip_round(toDecimalPlaces: 1) + XCTAssertEqual(pi_cgfloat, 3.1) + pi_cgfloat = .pi // Reset + pi_cgfloat.ip_round(toDecimalPlaces: 2) + XCTAssertEqual(pi_cgfloat, 3.14) + pi_cgfloat = .pi // Reset + pi_cgfloat.ip_round(toDecimalPlaces: 5) + XCTAssertEqual(pi_cgfloat, 3.14159) + } }