Last active
April 26, 2024 22:35
-
-
Save StewartLynch/2d8a00289f15200a9be6eaa57ac7c127 to your computer and use it in GitHub Desktop.
Uses the correct distance for the Locale you are in
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
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