Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if user prefers miles or kilometers?

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.

like image 918
Ilya Suzdalnitski Avatar asked Jul 27 '09 17:07

Ilya Suzdalnitski


People also ask

Why do we use miles instead of kilometers?

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.

Do Americans use km or miles?

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.

Do Americans use kilometers?

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.


4 Answers

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.

like image 178
RamwiseMatt Avatar answered Oct 12 '22 11:10

RamwiseMatt


To determine whether the user uses metric or not, NSLocale can tell you:

- (BOOL)isMetric {
  return [[[NSLocale currentLocale] objectForKey:NSLocaleUsesMetricSystem] boolValue];
}
like image 30
Chris Avatar answered Oct 12 '22 11:10

Chris


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.

like image 22
Mike Cooper Avatar answered Oct 12 '22 12:10

Mike Cooper


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
like image 41
Roboris Avatar answered Oct 12 '22 12:10

Roboris