diff --git a/CHANGELOG.md b/CHANGELOG.md index b95a8037..0685227a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +### 0.9.23 +- **Add**: Rounding for `Decimal`. +- **Add**: `doubleValue` property for `Decimal`. +- **Add**: `intValue` property for `Decimal`. +- **Fix**: Rounding for `Double`. + ### 0.9.22 - **Fix**: Make `Initializable` protocol public. diff --git a/LeadKit.podspec b/LeadKit.podspec index d982d83c..ae7a0d9a 100644 --- a/LeadKit.podspec +++ b/LeadKit.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "LeadKit" - s.version = "0.9.22" + s.version = "0.9.23" s.summary = "iOS framework with a bunch of tools for rapid development" s.homepage = "https://github.com/TouchInstinct/LeadKit" s.license = "Apache License, Version 2.0" diff --git a/LeadKit.xcodeproj/project.pbxproj b/LeadKit.xcodeproj/project.pbxproj index 3925b4c0..4078dba1 100644 --- a/LeadKit.xcodeproj/project.pbxproj +++ b/LeadKit.xcodeproj/project.pbxproj @@ -501,6 +501,8 @@ 8546C2E8224E864F0059C255 /* Error+NetworkExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8546C2E7224E864F0059C255 /* Error+NetworkExtensions.swift */; }; 8546C2E9224E864F0059C255 /* Error+NetworkExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8546C2E7224E864F0059C255 /* Error+NetworkExtensions.swift */; }; 8546C2EA224E864F0059C255 /* Error+NetworkExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8546C2E7224E864F0059C255 /* Error+NetworkExtensions.swift */; }; + 85A5D49522AA975000C7D254 /* Decimal+Rounding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85A5D49422AA975000C7D254 /* Decimal+Rounding.swift */; }; + 85A5D49C22AAB6B700C7D254 /* Decimal+Values.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85A5D49B22AAB6B700C7D254 /* Decimal+Values.swift */; }; A658E54D1F8CD7790093527A /* TableRow+SeparatorsExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = A658E54C1F8CD7790093527A /* TableRow+SeparatorsExtensions.swift */; }; A658E5501F8CD9350093527A /* Array+SeparatorRowBoxExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = A658E54F1F8CD9350093527A /* Array+SeparatorRowBoxExtensions.swift */; }; A676AE481F97D28A001F9214 /* String+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = A676AE471F97D28A001F9214 /* String+Extensions.swift */; }; @@ -774,6 +776,8 @@ 82F8BB171F5DDED100C1061B /* Single+DeferredJust.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Single+DeferredJust.swift"; sourceTree = ""; }; 8546C2E2224E86280059C255 /* ApiUploadRequestParameters.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ApiUploadRequestParameters.swift; sourceTree = ""; }; 8546C2E7224E864F0059C255 /* Error+NetworkExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Error+NetworkExtensions.swift"; sourceTree = ""; }; + 85A5D49422AA975000C7D254 /* Decimal+Rounding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Decimal+Rounding.swift"; sourceTree = ""; }; + 85A5D49B22AAB6B700C7D254 /* Decimal+Values.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Decimal+Values.swift"; sourceTree = ""; }; A658E54C1F8CD7790093527A /* TableRow+SeparatorsExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "TableRow+SeparatorsExtensions.swift"; sourceTree = ""; }; A658E54F1F8CD9350093527A /* Array+SeparatorRowBoxExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Array+SeparatorRowBoxExtensions.swift"; sourceTree = ""; }; A66428A61F8A653600C6308D /* SeparatorCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SeparatorCell.swift; sourceTree = ""; }; @@ -941,6 +945,7 @@ A6D10EA91F8A9269003E69DD /* Comparable */, 67745284206259C20024EEEF /* DataLoading */, 6771DFE81EEA7C8F002DCDAE /* DateFormattingService */, + 85A5D49322AA972B00C7D254 /* Decimal */, 671461EA1EB3396E00EAB194 /* Double */, 672947E2206EA59E00AC6B6B /* Drawing */, 8546C2E6224E864F0059C255 /* Error */, @@ -2019,6 +2024,15 @@ path = Error; sourceTree = ""; }; + 85A5D49322AA972B00C7D254 /* Decimal */ = { + isa = PBXGroup; + children = ( + 85A5D49422AA975000C7D254 /* Decimal+Rounding.swift */, + 85A5D49B22AAB6B700C7D254 /* Decimal+Values.swift */, + ); + path = Decimal; + sourceTree = ""; + }; A66428A41F8A651700C6308D /* SeparatorCell */ = { isa = PBXGroup; children = ( @@ -2415,6 +2429,7 @@ 671462D01EB3396E00EAB194 /* UIScrollView+Support.swift in Sources */, 671463901EB3396E00EAB194 /* TemplateDrawingOperation.swift in Sources */, A658E54D1F8CD7790093527A /* TableRow+SeparatorsExtensions.swift in Sources */, + 85A5D49522AA975000C7D254 /* Decimal+Rounding.swift in Sources */, 67EB7FCF20615B8900BDD9FB /* TotalCountCursorConfiguration.swift in Sources */, 671462981EB3396E00EAB194 /* CGSize+Resize.swift in Sources */, 67051ADB1EBC7C36008EADC0 /* SpinnerView.swift in Sources */, @@ -2461,6 +2476,7 @@ 67EB7FFD206176C900BDD9FB /* AnyPaginationWrappable.swift in Sources */, 671463041EB3396E00EAB194 /* UIView+LoadingIndicator.swift in Sources */, 674303CF214FB8F700EF4160 /* GeneralDataLoadingHandler.swift in Sources */, + 85A5D49C22AAB6B700C7D254 /* Decimal+Values.swift in Sources */, 6774527420624E820024EEEF /* DataLoadingModel.swift in Sources */, 40F118491F8FF223004AADAF /* TableRow+AppearanceExtension.swift in Sources */, 6727476E206CCDDB00725163 /* ViewBackground+Configuration.swift in Sources */, diff --git a/Sources/Extensions/Decimal/Decimal+Rounding.swift b/Sources/Extensions/Decimal/Decimal+Rounding.swift new file mode 100644 index 00000000..fbfa3ec4 --- /dev/null +++ b/Sources/Extensions/Decimal/Decimal+Rounding.swift @@ -0,0 +1,39 @@ +// +// Copyright (c) 2019 Touch Instinct +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// + +import Foundation + +public extension Decimal { + + /// Rounding of decimal value + /// + /// - Parameters: + /// - precision: significant digits after decimal point + /// - roundingMode: mode of rounding + /// - Returns: rounded value + func roundValue(precision: UInt, roundingMode: NSDecimalNumber.RoundingMode) -> Decimal { + var value = self + var rounded = Decimal() + NSDecimalRound(&rounded, &value, Int(precision), roundingMode) + return rounded + } +} diff --git a/Sources/Extensions/Decimal/Decimal+Values.swift b/Sources/Extensions/Decimal/Decimal+Values.swift new file mode 100644 index 00000000..ec3c07e6 --- /dev/null +++ b/Sources/Extensions/Decimal/Decimal+Values.swift @@ -0,0 +1,36 @@ +// +// Copyright (c) 2019 Touch Instinct +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// + +import Foundation + +public extension Decimal { + + /// Conver Decimal to Double value + var doubleValue: Double { + return NSDecimalNumber(decimal: self).doubleValue + } + + /// Conver Decimal to Int value + var intValue: Int { + return NSDecimalNumber(decimal: self).intValue + } +} diff --git a/Sources/Extensions/Double/Double+Rounding.swift b/Sources/Extensions/Double/Double+Rounding.swift index 3bb0380c..fd4234dd 100644 --- a/Sources/Extensions/Double/Double+Rounding.swift +++ b/Sources/Extensions/Double/Double+Rounding.swift @@ -23,32 +23,15 @@ import Foundation public extension Double { - /** - Type of rounding double value - - Normal: From 167.567 you will get 167.6 - - Down: From 167.567 you will get 167.5 - */ - enum RoundingType { - case normal - case down - } - /** - Rounding of double value - - parameter precision: significant digits after decimal point - - parameter roundType: rounding type - - returns: rounded value - */ - func roundValue(withPrecision precision: UInt, - roundType: RoundingType = .normal) -> Double { - let divider = pow(10.0, Double(precision)) - - switch roundType { - case .normal: - return (self * divider).rounded(.toNearestOrEven) / divider - - case .down: - return (self * divider).rounded(.down) / divider - } + /// Rounding of double value + /// + /// - Parameters: + /// - precision: significant digits after decimal point + /// - roundingMode: mode of rounding + /// - Returns: rounded value + func roundValue(precision: UInt, roundingMode: NSDecimalNumber.RoundingMode) -> Double { + // Using decimal here because of precision problem + return Decimal(self).roundValue(precision: precision, roundingMode: roundingMode).doubleValue } } diff --git a/Sources/Info-iOS.plist b/Sources/Info-iOS.plist index a8ca2d1d..4b8f7be2 100644 --- a/Sources/Info-iOS.plist +++ b/Sources/Info-iOS.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 0.9.19 + 0.9.23 CFBundleVersion $(CURRENT_PROJECT_VERSION) NSPrincipalClass diff --git a/Sources/Info-tvOS.plist b/Sources/Info-tvOS.plist index a8ca2d1d..4b8f7be2 100644 --- a/Sources/Info-tvOS.plist +++ b/Sources/Info-tvOS.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 0.9.19 + 0.9.23 CFBundleVersion $(CURRENT_PROJECT_VERSION) NSPrincipalClass diff --git a/Sources/Info-watchOS.plist b/Sources/Info-watchOS.plist index a8ca2d1d..4b8f7be2 100644 --- a/Sources/Info-watchOS.plist +++ b/Sources/Info-watchOS.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 0.9.19 + 0.9.23 CFBundleVersion $(CURRENT_PROJECT_VERSION) NSPrincipalClass