Search code examples
c#.netlocalizationinternationalizationglobalization

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?


Solution

  • 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");