-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'release/3.3.0' into versions
- Loading branch information
Showing
12 changed files
with
151 additions
and
18 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright © 2020 Flinesoft. All rights reserved. | ||
|
||
import Foundation | ||
|
||
extension Double { | ||
/// Rounds the value to an integral value using the specified fraction digits and rounding rule. | ||
/// | ||
/// - NOTE: Dropping the `rule` parameter will default to “schoolbook rounding”. | ||
public mutating func round(fractionDigits: Int, rule: FloatingPointRoundingRule = .toNearestOrAwayFromZero) { | ||
let divisor = pow(10.0, Double(fractionDigits)) | ||
self = (self * divisor).rounded(rule) / divisor | ||
} | ||
|
||
/// Returns this value rounded to an integral value using the specified fraction digits and rounding rule. | ||
/// | ||
/// - NOTE: Dropping the `rule` parameter will default to “schoolbook rounding”. | ||
public func rounded(fractionDigits: Int, rule: FloatingPointRoundingRule = .toNearestOrAwayFromZero) -> Double { | ||
let divisor = pow(10.0, Double(fractionDigits)) | ||
return (self * divisor).rounded(rule) / divisor | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright © 2020 Flinesoft. All rights reserved. | ||
|
||
import Foundation | ||
|
||
extension StringProtocol { | ||
/// Returns a variation with the first character uppercased. | ||
public var firstUppercased: String { prefix(1).uppercased() + dropFirst() } | ||
|
||
/// Returns a variation with the first character capitalized. | ||
public var firstCapitalized: String { prefix(1).capitalized + dropFirst() } | ||
|
||
/// Returns a variation with the first character lowercased. | ||
public var firstLowercased: String { prefix(1).lowercased() + dropFirst() } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright © 2020 Flinesoft. All rights reserved. | ||
|
||
@testable import HandySwift | ||
import XCTest | ||
|
||
class DoubleExtTests: XCTestCase { | ||
func testRound() { | ||
var price: Double = 2.875 | ||
price.round(fractionDigits: 2) | ||
XCTAssertEqual(price, 2.88) | ||
|
||
price = 2.875 | ||
price.round(fractionDigits: 2, rule: .down) | ||
XCTAssertEqual(price, 2.87) | ||
} | ||
|
||
func testRounded() { | ||
let price: Double = 2.875 | ||
XCTAssertEqual(price.rounded(fractionDigits: 2), 2.88) | ||
XCTAssertEqual(price.rounded(fractionDigits: 2, rule: .down), 2.87) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.