From f0fb443f6b8e7a5bcb042d9eee374c0f6328fcad Mon Sep 17 00:00:00 2001 From: Sasha Malina Date: Thu, 10 May 2018 12:28:48 +0300 Subject: [PATCH] Fix double rounding issue & typo In current state "normal" rounding type produce error: `3.14.roundValue(withPrecision: 0) == 4`, `3.14.roundValue(withPrecision: 1) == 3.2`, looks strange. Fix typo in word `precision`, improve description --- .../Extensions/Double/Double+Rounding.swift | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/Sources/Extensions/Double/Double+Rounding.swift b/Sources/Extensions/Double/Double+Rounding.swift index 09a406ad..860b31ad 100644 --- a/Sources/Extensions/Double/Double+Rounding.swift +++ b/Sources/Extensions/Double/Double+Rounding.swift @@ -23,10 +23,10 @@ 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 */ @@ -34,25 +34,26 @@ public extension Double { case normal case down } - + /** Rounding of double value - - - parameter persicion: important number of digits after comma + + - parameter precision: significant digits after decimal point - parameter roundType: rounding type - + - returns: rounded value */ - func roundValue(withPersicion persicion: UInt, + func roundValue(withPrecision precision: UInt, roundType: RoundingType = .normal) -> Double { - let divider = pow(10.0, Double(persicion)) - + let divider = pow(10.0, Double(precision)) + switch roundType { case .normal: - return (self * divider).rounded(.up) / divider + return (self * divider).rounded(.toNearestOrEven) / divider case .down: return (self * divider).rounded(.down) / divider } } - + } +