Accessing "Measurement System" unit names (kg/lb, m/in, etc) in C#
Asked Answered
H

2

10

Windows allows configuration of Measurement system to Metric or U.S. Is there a way to use this setting to read (abbreviated) unit names in C#?

e.g. when displaying a weight in Metric I want to show kg but in U.S. I want to show lb. Similarly for length, volume, etc.

I've looked at SystemInformation, CultureInfo, Configuration, and Globalization, but didn't see anything obvious. Did I miss something or am I looking in the wrong place?

Helium answered 27/10, 2009 at 8:51 Comment(0)
O
8

I believe the best you can do is to determine if your culture is metric or not and then handle it yourself. I don't think there's any built in formatting? I may be wrong though, but I cannot find any reference to it anywhere.

This will allow you to determine if your culture is metric or not:

    CultureInfo culture = new CultureInfo("en-GB");
    RegionInfo regionInfo = new RegionInfo(culture.LCID);
    bool isMetric = regionInfo.IsMetric;
Overage answered 27/10, 2009 at 9:27 Comment(2)
@GenericTypeTea, thanks it seems like this is the way to go. Seems odd that configuring metric/us doesn't give a way to access the units, I wonder what it's used for ... something to investigate when I'm bored :)Helium
If you find a better answer, be sure to let us know.Overage
C
-1

@GenericTypeTea is right about the RegionInfo being the place to look.

Additionally F# contains a lot of features for working with units of measure. It might make sense to build some of what you need in F# and call it from C#.

See this blog post and the subsequent posts for information about F# and units of measure.

Chippewa answered 27/10, 2009 at 9:50 Comment(1)
@Mike Two, thanks I read the blog post and there are some posts on SO about it also. The units stuff in F# looks interesting but it appears you still have to know the units you're using (e.g. you have to know you're using kg vs lb) which is what I wanted to get from the system configuration. I'm not familiar with F# though, so I may have misunderstood what I was reading.Helium

© 2022 - 2024 — McMap. All rights reserved.