Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace ip_round extension on Double for BinaryFloatingPoint #161

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 12 additions & 40 deletions SwiftWisdom.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand All @@ -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 */; };
Expand All @@ -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 */; };
Expand Down Expand Up @@ -131,7 +132,7 @@

/* Begin PBXFileReference section */
0922A8521FAD0A78005AD966 /* UIView+SafeArea.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIView+SafeArea.swift"; sourceTree = "<group>"; };
0CED7D661CD6971400672913 /* Double+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Double+Extensions.swift"; sourceTree = "<group>"; };
0CED7D661CD6971400672913 /* BinaryFloatingPoint+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "BinaryFloatingPoint+Extensions.swift"; sourceTree = "<group>"; };
13425F631DB073F300F446BE /* Sequence+UtilitiesTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Sequence+UtilitiesTests.swift"; sourceTree = "<group>"; };
13425F691DB073F300F446BE /* MathableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MathableTests.swift; sourceTree = "<group>"; };
13425F6B1DB073F300F446BE /* RawRepresentableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RawRepresentableTests.swift; sourceTree = "<group>"; };
Expand All @@ -140,7 +141,7 @@
13425F7B1DB0755B00F446BE /* NSMutableAttributedString+Format.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSMutableAttributedString+Format.swift"; sourceTree = "<group>"; };
13425F7D1DB0757000F446BE /* NSMutableAttributedString+FormatTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "NSMutableAttributedString+FormatTests.swift"; path = "NSAttributedString/NSMutableAttributedString+FormatTests.swift"; sourceTree = "<group>"; };
13425F801DB075A000F446BE /* Integer+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Integer+Extensions.swift"; sourceTree = "<group>"; };
13425F821DB075B300F446BE /* Double+ExtensionsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Double+ExtensionsTests.swift"; sourceTree = "<group>"; };
13425F821DB075B300F446BE /* BinaryFloatingPoint+ExtensionsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "BinaryFloatingPoint+ExtensionsTests.swift"; sourceTree = "<group>"; };
13425F831DB075B300F446BE /* Integer+ExtensionsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Integer+ExtensionsTests.swift"; sourceTree = "<group>"; };
13425F841DB075B300F446BE /* UnsignedInteger+ExtensionsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UnsignedInteger+ExtensionsTests.swift"; sourceTree = "<group>"; };
13425F881DB075E000F446BE /* String+Indexing.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+Indexing.swift"; sourceTree = "<group>"; };
Expand All @@ -165,6 +166,7 @@
1CE3D0671C6E61A500FB676C /* Mathable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Mathable.swift; sourceTree = "<group>"; };
1CFEBD041E8047D60078108A /* DateFormattingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = DateFormattingTests.swift; path = Date/DateFormattingTests.swift; sourceTree = "<group>"; };
1CFEBD071E8055AB0078108A /* UIButtonTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIButtonTests.swift; sourceTree = "<group>"; };
211E2D93222049D20073A9C8 /* Double+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Double+Extensions.swift"; sourceTree = "<group>"; };
311178581E16CACF00294C25 /* String+Attributed.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+Attributed.swift"; sourceTree = "<group>"; };
314F3BF51E9EA7E7005753BE /* VideoPlayerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VideoPlayerTests.swift; sourceTree = "<group>"; };
314F3BF71E9EA92B005753BE /* testvideoone.mp4 */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = testvideoone.mp4; sourceTree = "<group>"; };
Expand Down Expand Up @@ -306,7 +308,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 */,
Expand Down Expand Up @@ -737,7 +739,8 @@
13425F801DB075A000F446BE /* Integer+Extensions.swift */,
80D63D2C1C4F251E00C88D00 /* UnsignedInteger+Extensions.swift */,
1CE3D0671C6E61A500FB676C /* Mathable.swift */,
0CED7D661CD6971400672913 /* Double+Extensions.swift */,
211E2D93222049D20073A9C8 /* Double+Extensions.swift */,
0CED7D661CD6971400672913 /* BinaryFloatingPoint+Extensions.swift */,
);
path = Numbers;
sourceTree = "<group>";
Expand Down Expand Up @@ -809,7 +812,6 @@
80232B9C1BF2EE2E00818B6E /* Frameworks */,
80232B9D1BF2EE2E00818B6E /* Resources */,
BEDBF20DB05D76F60C858449 /* [CP] Embed Pods Frameworks */,
E3F5EA111B900D6479C8D173 /* [CP] Copy Pods Resources */,
3169F10A1F06850F00C917B2 /* SwiftLint */,
);
buildRules = (
Expand All @@ -830,7 +832,6 @@
80232BB01BF2EE2E00818B6E /* Frameworks */,
80232BB11BF2EE2E00818B6E /* Resources */,
FAA5EC81369AE73A0E00ED4B /* [CP] Embed Pods Frameworks */,
77882022D65A5916A7FFD999 /* [CP] Copy Pods Resources */,
);
buildRules = (
);
Expand Down Expand Up @@ -938,21 +939,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 */ = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did this change? Did you re-run pod install If so, you could probably check in the Podfile.lock

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did run pod install to generate the workspace

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, since that made THIS change, didn't it also update Podfile.lock? You should rerun it and check in the lock file if it changed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the lock file did not change.

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;
Expand Down Expand Up @@ -993,21 +979,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;
Expand Down Expand Up @@ -1053,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 */,
Expand Down Expand Up @@ -1091,7 +1063,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 */,
Expand Down Expand Up @@ -1123,7 +1095,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 */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to give this link the - SeeAlso: tag in the documentation along with adding a basic summary of the function.

public func ip_rounded(toDecimalPlaces places: Int) -> Self {
guard places >= 0 else { return self }
let divisor = Self((0..<places).reduce(1.0) { (result, _) in result * 10.0 })
return (self * divisor).rounded() / divisor
}

/// https://codereview.stackexchange.com/questions/142748/swift-floatingpoint-rounded-to-places
public mutating func ip_round(toDecimalPlaces places: Int) {
guard places >= 0 else { return }
let divisor = Self((0..<places).reduce(1.0) { (result, _) in result * 10.0 })
self = (self * divisor).rounded() / divisor
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
// Double+Extensions.swift
// SwiftWisdom
//
// Created by Stephen Wong on 5/1/16.
// Copyright © 2016 Intrepid. All rights reserved.
// Created by Litteral, Maximilian on 2/22/19.
// Copyright © 2019 Intrepid. All rights reserved.
//

import Foundation

extension Double {
@available(*, unavailable, renamed: "ip_rounded(toDecimalPlaces:)")
public func ip_round(toDecimalPlaces decimalPlaces: Int) -> Double {
let decimalShiftBase10 = pow(10.0, Double(decimalPlaces))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this code now cannot be called, it should be removed. But since the method return a value, it needs to call fatalError or something similar.

let roundedNumber = (self * decimalShiftBase10).rounded()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//
// BinaryFloatingPointTests.swift
// SwiftWisdom
//
// Created by Stephen Wong on 5/1/16.
// Copyright © 2016 Intrepid. All rights reserved.
//

import XCTest
import SwiftWisdom

class BinaryFloatingPointTests: XCTestCase {

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_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)
}
}
Loading