Skip to content

Instantly share code, notes, and snippets.

@DavidBemerguy
Created July 17, 2023 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidBemerguy/8f83bca7fe2ce45458083d3ad72bfa0c to your computer and use it in GitHub Desktop.
Save DavidBemerguy/8f83bca7fe2ce45458083d3ad72bfa0c to your computer and use it in GitHub Desktop.
Specify the decimal place to round to using an enum
// 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