@@ -30,6 +30,7 @@ See [Upgrading from 3.x to 4.x](https://github.com/angularsen/UnitsNet/wiki/Upgr
30
30
* [ Operator overloads] ( #operator-overloads ) for arithmetic on quantities
31
31
* [ Parse and ToString()] ( #culture ) supports cultures and localization
32
32
* [ Dynamically parse and convert] ( #dynamic-parsing ) quantities and units
33
+ * [ Custom units] ( #custom-units )
33
34
* [ Example: Creating a unit converter app] ( #example-app )
34
35
* [ Example: WPF app using IValueConverter to parse quantities from input] ( #example-wpf-app-using-ivalueconverter-to-parse-quantities-from-input )
35
36
* [ Precision and accuracy] ( #precision )
@@ -225,6 +226,36 @@ UnitConverter.ConvertByName(1, "Length", "Centimeter", "Millimeter"); // 10 mm
225
226
UnitConverter .ConvertByAbbreviation (1 , " Length" , " cm" , " mm" ); // 10 mm
226
227
```
227
228
229
+ ### <a name="custom-units"></a>Custom units
230
+
231
+ Units .NET allows you to add your own units and quantities at runtime , to represent as `IQuantity ` and reusing Units .NET for parsing and converting between units .
232
+
233
+ Read more at [Extending - with - Custom - Units ](https :// github.com/angularsen/UnitsNet/wiki/Extending-with-Custom-Units).
234
+
235
+ #### Map between unit enum values and unit abbreviations
236
+ ```c #
237
+ // Map unit enum values to unit abbreviations
238
+ UnitAbbreviationsCache .Default .MapUnitToDefaultAbbreviation (HowMuchUnit .Some , " sm" );
239
+ UnitAbbreviationsCache .Default .GetDefaultAbbreviation (HowMuchUnit .Some ); // "sm"
240
+ UnitParser .Default .Parse <HowMuchUnit >(" sm" ); // HowMuchUnit.Some
241
+
242
+ var unitConverter = UnitConverter .Default ;
243
+ unitConverter .SetConversionFunction <HowMuch >(HowMuchUnit .Lots , HowMuchUnit .Some , x => new HowMuch (x .Value * 2 , HowMuchUnit .Some ));
244
+ unitConverter .SetConversionFunction <HowMuch >(HowMuchUnit .Tons , HowMuchUnit .Lots , x => new HowMuch (x .Value * 10 , HowMuchUnit .Lots ));
245
+ unitConverter .SetConversionFunction <HowMuch >(HowMuchUnit .Tons , HowMuchUnit .Some , x => new HowMuch (x .Value * 20 , HowMuchUnit .Some ));
246
+ ```
247
+
248
+ #### Convert between units of custom quantity
249
+ ```c #
250
+ var from = new HowMuch (10 , HowMuchUnit .Tons );
251
+ IQuantity Convert (HowMuchUnit toUnit ) => unitConverter .GetConversionFunction <HowMuch >(from .Unit , toUnit )(from );
252
+
253
+ Console .WriteLine ($" Convert 10 tons to:" );
254
+ Console .WriteLine (Convert (HowMuchUnit .Some )); // 200 sm
255
+ Console .WriteLine (Convert (HowMuchUnit .Lots )); // 100 lts
256
+ Console .WriteLine (Convert (HowMuchUnit .Tons )); // 10 tns
257
+ ```
258
+
228
259
### <a name="example-app"></a>Example: Creating a dynamic unit converter app
229
260
[Source code ](https :// github.com/angularsen/UnitsNet/tree/master/Samples/UnitConverter.Wpf) for `Samples/UnitConverter.Wpf`<br/>
230
261
[Download ](https :// github.com/angularsen/UnitsNet/releases/tag/UnitConverterWpf%2F2018-11-09) (release 2018-11-09 for Windows)
0 commit comments