In my app I've got a certain distance in meters.
And I want to display it in kilometers if user prefers kilometers and display it in miles if user prefers miles. And in the first case I want to add to a string "kilometers" at the end and in the second one to add "miles".
What is the best way to achieve this goal?
Thanks.
When the English settlers came to America, they were using Imperial units. Many years after the United States declared independence, England moved to the metric system, but the US did not. Previous attempts to adopt the metric system were treated like communist invasions, so people decided to keep the same units.
The United States is the only real stronghold of the imperial system in the world to-date. Here, using miles and gallons is the norm, even though scientists do use metric, new units like megabytes and megapixels are metric as well and runners compete for 100 meters like everywhere else in the world.
Across all major age groups, majorities would use miles per hour to describe the speed of a vehicle, but American adults under 45 are the most likely to say they would use kilometers per hour, at 15%. No more than 3% of those over 45 would use kilometers.
Swift equivalent of Chris' answer would be something like this:
func isMetric() -> Bool {
return ((Locale.current as NSLocale).object(forKey: NSLocale.Key.usesMetricSystem) as? Bool) ?? true
}
Note that it defaults to true
under certain circumstances. Change as needed.
To determine whether the user uses metric or not, NSLocale can tell you:
- (BOOL)isMetric {
return [[[NSLocale currentLocale] objectForKey:NSLocaleUsesMetricSystem] boolValue];
}
You could ask the user whether they prefer miles or kilometers, in a preference or something. Then whenever you display a distance you would say.
In pseudo c code
function distance(meters) {
if (userPrefersKM) {
return meters / 1000 + " kilometers";
else if (userPrefersMiles) {
return meters / METERS_IN_A_MILE + " miles";
}
Where METERS_IN_A_MILE
would be about 1600, but you should look that up.
In Swift, Locale.current.usesMetricSystem
gives what the user would expect. But you don't need that if you use Measurement
which handles it for you.
let distanceInMeters: Double = 2353.45
let formatter = MeasurementFormatter()
formatter.unitStyle = .medium // adjust according to your need
let distance = Measurement(value: distanceInMeters, unit: UnitLength.meters)
formatter.string(from: distance)
The current locale dictates how it is presented to the user. To see how it works for different locales, try this in a Xcode Playground (examples are for UK and France):
let distanceInMeters: Double = 2353.45
let formatter = MeasurementFormatter()
formatter.unitStyle = .medium // adjust according to your need
let distance = Measurement(value: distanceInMeters, unit: UnitLength.meters)
formatter.locale = Locale(identifier: "en_UK")
formatter.string(from: distance) // 1.462 mi
formatter.locale = Locale(identifier: "en_FR")
formatter.string(from: distance) // 2,353 km
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With