Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to tell if the user would prefer metric or imperial without asking in C#?

Right now I'm doing:

bool UseMetricByDefault() {
    return TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).TotalHours >= 0;
}

This works for distinguishing USA from Europe and Asia, but it ignores South America.

Is there a better way?

like image 510
Instance Hunter Avatar asked Dec 10 '09 16:12

Instance Hunter


1 Answers

Use the RegionInfo class in the System.Globalization namespace:

bool isMetric = RegionInfo.CurrentRegion.IsMetric;

If you want a specific region, you can use one of the following:

// from a CultureInfo
CultureInfo culture = ...;
RegionInfo r = new RegionInfo(culture.Name);

// from a string
RegionInfo r = new RegionInfo("us");
like image 56
Wim Avatar answered Sep 27 '22 22:09

Wim