Skip to content

Commit efaf685

Browse files
authored
Update README.md
1 parent d89306b commit efaf685

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ See [Upgrading from 3.x to 4.x](https://github.com/angularsen/UnitsNet/wiki/Upgr
3030
* [Operator overloads](#operator-overloads) for arithmetic on quantities
3131
* [Parse and ToString()](#culture) supports cultures and localization
3232
* [Dynamically parse and convert](#dynamic-parsing) quantities and units
33+
* [Custom units](#custom-units)
3334
* [Example: Creating a unit converter app](#example-app)
3435
* [Example: WPF app using IValueConverter to parse quantities from input](#example-wpf-app-using-ivalueconverter-to-parse-quantities-from-input)
3536
* [Precision and accuracy](#precision)
@@ -225,6 +226,36 @@ UnitConverter.ConvertByName(1, "Length", "Centimeter", "Millimeter"); // 10 mm
225226
UnitConverter.ConvertByAbbreviation(1, "Length", "cm", "mm"); // 10 mm
226227
```
227228

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+
228259
### <a name="example-app"></a>Example: Creating a dynamic unit converter app
229260
[Source code](https://github.com/angularsen/UnitsNet/tree/master/Samples/UnitConverter.Wpf) for `Samples/UnitConverter.Wpf`<br/>
230261
[Download](https://github.com/angularsen/UnitsNet/releases/tag/UnitConverterWpf%2F2018-11-09) (release 2018-11-09 for Windows)

0 commit comments

Comments
 (0)