Created
July 17, 2023 10:38
-
-
Save DavidBemerguy/8f83bca7fe2ce45458083d3ad72bfa0c to your computer and use it in GitHub Desktop.
Specify the decimal place to round to using an enum
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
// Specify the decimal place to round to using an enum | |
public enum RoundingPrecision { | |
case ones | |
case tenths | |
case hundredths | |
} | |
// Round to the specific decimal place | |
public func preciseRound( | |
_ value: Double, | |
precision: RoundingPrecision = .ones) -> Double | |
{ | |
switch precision { | |
case .ones: | |
return round(value) | |
case .tenths: | |
return round(value * 10) / 10.0 | |
case .hundredths: | |
return round(value * 100) / 100.0 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment