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
This commit is contained in:
Sasha Malina 2018-05-10 12:28:48 +03:00
parent aff508ed44
commit f0fb443f6b
1 changed files with 12 additions and 11 deletions

View File

@ -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
}
}
}