Skip to content

Instantly share code, notes, and snippets.

@ekurutepe
Last active May 18, 2020 09:06
Show Gist options
  • Save ekurutepe/395e7d8fe1555c2d560ac5b72e743df5 to your computer and use it in GitHub Desktop.
Save ekurutepe/395e7d8fe1555c2d560ac5b72e743df5 to your computer and use it in GitHub Desktop.
Conversions in Measurement too accurate for you? Here take this…
import Foundation
extension Measurement where UnitType: Dimension {
func approximatelyConverted(to otherUnit: UnitType) -> Measurement<UnitType> {
switch (unit, otherUnit) {
case (UnitLength.meters, UnitLength.feet):
return Measurement(value: value * 3, unit: otherUnit)
case (UnitLength.feet, UnitLength.meters):
return Measurement(value: value * 3 / 10, unit: otherUnit)
case (UnitSpeed.knots, UnitSpeed.metersPerSecond):
return Measurement(value: value / 2.0, unit: otherUnit)
case (UnitSpeed.metersPerSecond, UnitSpeed.knots):
return Measurement(value: value * 2.0, unit: otherUnit)
default:
return converted(to: otherUnit)
}
}
}
@ekurutepe
Copy link
Author

Disclaimer: This will introduce all kind of errors in your conversions. Do NOT use in cases where you actually need the accuracy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment