Skip to content

Instantly share code, notes, and snippets.

@StewartLynch
Last active April 26, 2024 22:35
Show Gist options
  • Save StewartLynch/2d8a00289f15200a9be6eaa57ac7c127 to your computer and use it in GitHub Desktop.
Save StewartLynch/2d8a00289f15200a9be6eaa57ac7c127 to your computer and use it in GitHub Desktop.
Uses the correct distance for the Locale you are in
static func distance(meters: Double) -> String {
let userLocale = Locale.current
let formatter = MeasurementFormatter()
var options: MeasurementFormatter.UnitOptions = []
options.insert(.providedUnit)
options.insert(.naturalScale)
formatter.unitOptions = options
let meterValue = Measurement(value: meters, unit: UnitLength.meters)
let yardsValue = Measurement(value: meters, unit: UnitLength.yards)
return formatter.string(from: userLocale.measurementSystem == .metric ? meterValue : yardsValue)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment