@@ -44,19 +44,25 @@ Run the following command in the [Package Manager Console](http://docs.nuget.org
44
44
### <a name =" static-typing " ></a >Static Typing
45
45
46
46
``` C#
47
- // Convert to the unit of choice - when you need it
48
- Mass weight = GetPersonWeight ();
49
- Console .WriteLine (" You weigh {0:0.#} kg." , weight .Kilograms );
50
-
51
- // Avoid confusing conversions, such as between weight (force) and mass
52
- double weightNewtons = weight .Newtons ; // No such thing
53
-
54
- // Some popular conversions
47
+ // Construct
55
48
Length meter = Length .FromMeters (1 );
56
- double cm = meter .Centimeters ; // 100
57
- double yards = meter .Yards ; // 1.09361
58
- double feet = meter .Feet ; // 3.28084
59
- double inches = meter .Inches ; // 39.3701
49
+ Length twoMeters = new Length (2 , LengthUnit .Meter );
50
+
51
+ // Convert
52
+ double cm = meter .Centimeters ; // 100
53
+ double yards = meter .Yards ; // 1.09361
54
+ double feet = meter .Feet ; // 3.28084
55
+ double inches = meter .Inches ; // 39.3701
56
+
57
+ // Pass quantity types instead of values to avoid conversion mistakes and communicate intent
58
+ string PrintPersonWeight (Mass weight )
59
+ {
60
+ // No such thing! Weight in this context is Mass, not Force.
61
+ double weightNewtons = weight .Newtons ;
62
+
63
+ // Convert to the unit of choice - when you need it
64
+ return $" You weigh {weight .Kilograms : F1 } kg." ;
65
+ }
60
66
```
61
67
62
68
### <a name =" operator-overloads " ></a >Operator Overloads
@@ -87,23 +93,23 @@ var usEnglish = new CultureInfo("en-US");
87
93
var russian = new CultureInfo (" ru-RU" );
88
94
var oneKg = Mass .FromKilograms (1 );
89
95
90
- // Honors Thread. CurrentUICulture
91
- Thread .CurrentUICulture = russian ;
96
+ // ToString() uses CurrentUICulture for abbreviation language and CurrentCulture for number formatting
97
+ Thread .CurrentThread . CurrentUICulture = russian ;
92
98
string kgRu = oneKg .ToString (); // "1 кг"
93
99
94
- // ToString() with specific culture and string format pattern
95
- string mgUs = oneKg .ToString (MassUnit .Milligram , usEnglish , " {1} {0:0.00 }" ); // "mg 1.00"
96
- string mgRu = oneKg .ToString (MassUnit .Milligram , russian , " {1} {0:0.00 }" ); // "мг 1,00"
100
+ // ToString() with specific culture and custom string format pattern
101
+ string mgUs = oneKg .ToUnit (MassUnit .Milligram ). ToString ( usEnglish , " unit: {1}, value: {0:F2 }" ); // "unit: mg, value: 1.00"
102
+ string mgRu = oneKg .ToUnit (MassUnit .Milligram ). ToString ( russian , " unit: {1}, value: {0:F2 }" ); // "unit: мг, value: 1,00"
97
103
98
104
// Parse measurement from string
99
- Mass kg = Mass .Parse (usEnglish , " 1.0 kg" );
105
+ Mass kg = Mass .Parse (" 1.0 kg" , usEnglish );
100
106
101
107
// Parse unit from string, a unit can have multiple abbreviations
102
- RotationalSpeedUnit rpm1 == RotationalSpeed .ParseUnit (" rpm" ); // RotationalSpeedUnit.RevolutionPerMinute
103
- RotationalSpeedUnit rpm2 == RotationalSpeed .ParseUnit (" r/min" ); // RotationalSpeedUnit.RevolutionPerMinute
108
+ RotationalSpeedUnit rpm1 = RotationalSpeed .ParseUnit (" rpm" ); // RotationalSpeedUnit.RevolutionPerMinute
109
+ RotationalSpeedUnit rpm2 = RotationalSpeed .ParseUnit (" r/min" ); // RotationalSpeedUnit.RevolutionPerMinute
104
110
105
- // Get default abbreviation for a unit
106
- string abbrevKg = Mass .GetAbbreviation (MassUnit .Kilogram ); // "kg"
111
+ // Get default abbreviation for a unit, the first if more than one is defined in Length.json for Kilogram unit
112
+ string kgAbbreviation = Mass .GetAbbreviation (MassUnit .Kilogram ); // "kg"
107
113
```
108
114
109
115
#### Gotcha: AmbiguousUnitParseException
@@ -113,6 +119,14 @@ Unfortunately there is no built-in way to avoid this, either you need to ensure
113
119
Example:
114
120
` Length.Parse("1 pt") ` throws ` AmbiguousUnitParseException ` with message ` Cannot parse "pt" since it could be either of these: DtpPoint, PrinterPoint ` .
115
121
122
+ ### Dynamically Parsing Quantities and Units
123
+ Sometimes you need to work with quantities and units at runtime, such as parsing user input.
124
+ There are three classes to help with this:
125
+
126
+ ``` c#
127
+
128
+ ```
129
+
116
130
117
131
### <a name =" example-app " ></a >Example: Creating a dynamic unit converter app
118
132
[ Source code] ( https://github.com/angularsen/UnitsNet/tree/master/Samples/UnitConverter.Wpf ) for ` Samples/UnitConverter.Wpf ` <br />
@@ -140,10 +154,6 @@ LengthUnit[] lengthUnits = Length.Units;
140
154
double inputValue ; // Obtain from textbox
141
155
LengthUnit fromUnit , toUnit ; // Obtain from ListBox selections
142
156
double resultValue = Length .From (inputValue , fromUnit ).As (toUnit );
143
-
144
- // Alternatively, you can also convert using string representations of units
145
- double centimeters = UnitConverter .ConvertByName (5 , " Length" , " Meter" , " Centimeter" ); // 500
146
- double centimeters2 = UnitConverter .ConvertByAbbreviation (5 , " Length" , " m" , " cm" ); // 500
147
157
```
148
158
149
159
### Example: WPF app using IValueConverter to parse quantities from input
0 commit comments