|
1 |
| -Units.NET |
2 |
| -======== |
3 |
| - |
4 |
| -Everyone have written their share of trivial conversions - or less obvious ones where you need to Google that magic constant. |
5 |
| - |
6 |
| -Stop littering your code with unnecessary calculations. Units.NET gives you all the common units of measurement and the conversions between them. It is light-weight, unit tested and supports [PCL](http://msdn.microsoft.com/en-us/library/gg597391.aspx "MSDN PCL"). |
7 |
| - |
8 |
| - |
9 |
| -Install |
10 |
| -======= |
11 |
| -To install Units.NET, run the following command in the [Package Manager Console](http://docs.nuget.org/docs/start-here/using-the-package-manager-console) or go to the [NuGet site](https://www.nuget.org/packages/UnitsNet/ "NuGet site") for the complete relase history. |
12 |
| - |
13 |
| - |
14 |
| - |
15 |
| -Build Targets: |
16 |
| -* .NET 3.5 Client |
17 |
| -* Silverlight 4 |
18 |
| -* WinRT / .NET Core 4.5 |
19 |
| -* Portable Class Library (.NET 4.0 + Silverlight 4 + Windows Phone 7 + Xbox 360) |
20 |
| - |
21 |
| -Features |
22 |
| -======== |
23 |
| - |
24 |
| -* Immutable structs for units of measurement, such as Length, Mass, Force and Pressure. See full list [here](https://github.com/InitialForce/UnitsNet/blob/master/Src/UnitsNet/ "Data structures"). |
25 |
| -* Convert between most popular units in the metric and imperial systems. See full list [here](https://github.com/InitialForce/UnitsNet/blob/master/Src/UnitsNet/Unit.cs "Unit.cs"). |
26 |
| -* Choose between static (Length, Mass, Force etc.) or dynamic (UnitValue) representations for units of measurement. |
27 |
| -* Parse abbreviation string to unit taking culture into account. |
28 |
| -* Get abbreviation string for unit in different cultures. |
29 |
| - |
30 |
| -Static Representation and Explicit Conversion |
31 |
| ------------------------------------------------ |
32 |
| -```C# |
33 |
| -// Stop postfixing your variables and method names with the unit... |
34 |
| -double weightKg = GetPersonWeightInKg(); |
35 |
| -UpdatePersonWeightInGrams(weightKg * 1000); |
36 |
| - |
37 |
| -// ...and start using a static representation for the measurement then |
38 |
| -// explicitly convert to the unit of choice - when you need it. |
39 |
| -Mass weight = GetPersonWeight(); |
40 |
| -UpdatePersonWeightInGrams(weight.Grams); |
41 |
| - |
42 |
| -// Convert between compatible units of measurement... |
43 |
| -Force scaleMeasurement = Force.FromNewtons(850); |
44 |
| -Mass personWeight = Mass.FromGravitationalForce(scaleMeasurement); |
45 |
| -double weightKg = personWeight.Kilograms; |
46 |
| - |
47 |
| -// ...while avoiding confusing conversions, such as between weight and mass. |
48 |
| -Mass weight = GetPersonWeight(); |
49 |
| -double weightNewtons = weight.Newtons; // No such thing. |
50 |
| -
|
51 |
| -// Some popular conversions. |
52 |
| -Length meter = Length.FromMeters(1); |
53 |
| -double cm = meter.Centimeters; // 100 |
54 |
| -double yards = meter.Yards; // 1.09361 |
55 |
| -double feet = meter.Feet; // 3.28084 |
56 |
| -double inches = meter.Inches; // 39.3701 |
57 |
| -
|
58 |
| -Pressure p = Pressure.FromPascal(1); |
59 |
| -double kpa = p.KiloPascals; // 1×10-3 |
60 |
| -double bar = p.Bars; // 1×10-5 |
61 |
| -double atm = p.Atmosphere; // 9.86923267×10-6 |
62 |
| -double psi = p.Psi; // 1.45037738×10-4 |
63 |
| -``` |
64 |
| - |
65 |
| -Dynamic Representation and Conversion |
66 |
| ------------------- |
67 |
| -```C# |
68 |
| -// Explicitly |
69 |
| -double m = UnitConverter.Convert(1, Unit.Kilometer, Unit.Meter); // 1000 |
70 |
| -double mi = UnitConverter.Convert(1, Unit.Kilometer, Unit.Mile); // 0.621371 |
71 |
| -double yds = UnitConverter.Convert(1, Unit.Meter, Unit.Yard); // 1.09361 |
72 |
| -
|
73 |
| -// Or dynamically. |
74 |
| -UnitValue val = GetUnknownValueAndUnit(); |
75 |
| - |
76 |
| -// Returns false if conversion was not possible. |
77 |
| -double cm; |
78 |
| -val.TryConvert(Unit.Centimeter, out cm); |
79 |
| -``` |
80 |
| - |
81 |
| -Helper Methods to Construct Measurements |
82 |
| ----------------------------------------- |
83 |
| -```C# |
84 |
| -var f = Force.FromPressureByArea(Pressure p, Length2d area); |
85 |
| -var f = Force.FromMassAcceleration(Mass mass, double metersPerSecondSquared); |
86 |
| -``` |
87 |
| - |
88 |
| -Parse and Get Culture-Specific Abbreviations |
89 |
| -------------------------------------------------- |
90 |
| -```C# |
91 |
| - var us = new CultureInfo("en-US"); |
92 |
| - var norwegian = new CultureInfo("nb-NO"); |
93 |
| - |
94 |
| - Unit.Tablespoon == UnitSystem.Create(us).Parse("tbsp") |
95 |
| - Unit.Tablespoon == UnitSystem.Create(norwegian).Parse("ss") |
96 |
| - |
97 |
| - "T" == UnitSystem.GetDefaultAbbreviation(Unit.Tablespoon, us) |
98 |
| - "ss" == UnitSystem.GetDefaultAbbreviation(Unit.Tablespoon, norwegian) |
99 |
| -``` |
100 |
| - |
101 |
| -What It Is Not |
102 |
| -============== |
103 |
| - |
104 |
| -* It is not an equation solver. |
105 |
| -* It does not figure out the units after a calculation. |
106 |
| - |
107 |
| -Work In Progress |
108 |
| -================ |
109 |
| -This project is still early and many units and conversions are not yet covered. If you are missing something, please help by contributing. |
110 |
| - |
111 |
| -* Add more units. |
112 |
| -* Not all conversions are unit tested yet. |
113 |
| -* Parsing and getting textual representations not complete. |
114 |
| -* Document all the data structures, units and conversions. |
115 |
| - |
116 |
| -Want To Contribute? |
117 |
| -=================== |
118 |
| -Please read the wiki on [Adding a New Unit](https://github.com/InitialForce/UnitsNet/wiki/Adding-a-New-Unit) |
119 |
| -and [Adding a New Class of Units](https://github.com/InitialForce/UnitsNet/wiki/Adding-a-New-Class-of-Units).<br> |
120 |
| -For other things to do, see Work In Progress. |
121 |
| - |
122 |
| - |
123 |
| -[Contact me](https://github.com/anjdreas) if you have any questions. |
| 1 | +[/statusIcon)](http://anj.no:8500/viewType.html?buildTypeId=UnitsNet_ReleaseBuilds&guest=1 "Build Status") |
| 2 | +Units.NET |
| 3 | +=== |
| 4 | +Everyone have written their share of trivial conversions - or less obvious ones where you need to Google that magic constant. |
| 5 | + |
| 6 | +Stop littering your code with unnecessary calculations. Units.NET gives you all the common units of measurement and the conversions between them. It is light-weight, unit tested and supports [PCL](http://msdn.microsoft.com/en-us/library/gg597391.aspx "MSDN PCL"). |
| 7 | + |
| 8 | + |
| 9 | +Installing |
| 10 | +=== |
| 11 | +Run the following command in the [Package Manager Console](http://docs.nuget.org/docs/start-here/using-the-package-manager-console) or go to the [NuGet site](https://www.nuget.org/packages/UnitsNet/) for the complete relase history. |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +Build Targets: |
| 16 | +* Portable Class Library (.NET 4.0 + Silverlight 5 + Win8/WinPhone8) |
| 17 | +* .NET 3.5 Client |
| 18 | + |
| 19 | +Features |
| 20 | +=== |
| 21 | +* 16 unit classes of measurement, [full list](https://github.com/InitialForce/UnitsNet/tree/develop/Src/UnitsNet/GeneratedCode/Enums) |
| 22 | +* Generated code for uniform implementations and fewer bugs |
| 23 | +* Immutable structs implementing IEquatable, IComparable and operator overloads |
| 24 | +* Parse unit abbreviations in multiple cultures |
| 25 | +* ToString() variants for custom cultures and format patterns |
| 26 | +* Extensible with [custom units](https://github.com/InitialForce/UnitsNet/wiki/Extending-with-Custom-Units) |
| 27 | +* Over 200 tests to ensure conversions and localizations are in order |
| 28 | + |
| 29 | +Static Typing |
| 30 | +--- |
| 31 | +```C# |
| 32 | +// Convert to the unit of choice - when you need it |
| 33 | +Mass weight = GetPersonWeight(); |
| 34 | +Console.WriteLine("You weigh {0:0.#} kg.", weight.Kilograms); |
| 35 | + |
| 36 | +// Avoid confusing conversions, such as between weight (force) and mass |
| 37 | +double weightNewtons = weight.Newtons; // No such thing |
| 38 | +
|
| 39 | +// Some popular conversions |
| 40 | +Length meter = Length.FromMeters(1); |
| 41 | +double cm = meter.Centimeters; // 100 |
| 42 | +double yards = meter.Yards; // 1.09361 |
| 43 | +double feet = meter.Feet; // 3.28084 |
| 44 | +double inches = meter.Inches; // 39.3701 |
| 45 | +``` |
| 46 | + |
| 47 | +Unit Enumeration |
| 48 | +--- |
| 49 | +All units have a corresponding unit enum value. This is useful when selecting the unit representation at runtime, such as presenting a choice of units to the user. |
| 50 | +```C# |
| 51 | +/// <summary>Convert the previous height to the new unit.</summary> |
| 52 | +void OnUserChangedHeightUnit(LengthUnit prevUnit, double prevValue, LengthUnit newUnit) |
| 53 | +{ |
| 54 | + // Construct from dynamic unit and value |
| 55 | + var prevHeight = Length.From(prevValue, prevUnit); |
| 56 | + |
| 57 | + // Convert to the new unit |
| 58 | + double newHeightValue = prevHeight.As(newUnit); |
| 59 | + |
| 60 | + // Update UI with the converted value and the newly selected unit |
| 61 | + UpdateHeightUI(newHeightValue, newUnit); |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +Culture and Localization |
| 66 | +--- |
| 67 | +The culture for abbreviations defaults to Thread.CurrentUICulture and falls back to US English if not defined. Thread.CurrentCulture affects number formatting unless a custom culture is specified. The relevant methods are: |
| 68 | + |
| 69 | +* ToString() |
| 70 | +* GetAbbreviation() |
| 71 | +* Parse/TryParse() |
| 72 | +* ParseUnit/TryParseUnit() |
| 73 | +```C# |
| 74 | +var usEnglish = new CultureInfo("en-US"); |
| 75 | +var russian = new CultureInfo("ru-RU"); |
| 76 | +var oneKg = Mass.FromKilograms(1); |
| 77 | + |
| 78 | +// ToString() with Thread.CurrentUICulture as US English and Russian respectively |
| 79 | +"1 kg" == oneKg.ToString(); |
| 80 | +"1 кг" == oneKg.ToString(); |
| 81 | + |
| 82 | +// ToString() with specific culture and string format pattern |
| 83 | +"mg 1.00" == oneKg.ToString(MassUnit.Milligram, usEnglish, "{1} {0:0.00}"); |
| 84 | +"мг 1,00" == oneKg.ToString(MassUnit.Milligram, russian, "{1} {0:0.00}"); |
| 85 | + |
| 86 | +// Parse measurement from string |
| 87 | +Mass kg = Mass.Parse(usEnglish, "1.0 kg"); |
| 88 | +Mass kg = Mass.Parse(russian, "1,0 кг"); |
| 89 | + |
| 90 | +// Parse unit from string, a unit can have multiple abbreviations |
| 91 | +RotationalSpeedUnit.RevolutionPerMinute == RotationalSpeed.ParseUnit("rpm"); |
| 92 | +RotationalSpeedUnit.RevolutionPerMinute == RotationalSpeed.ParseUnit("r/min"); |
| 93 | + |
| 94 | +// Get default abbreviation for a unit |
| 95 | +"kg" == Mass.GetAbbreviation(MassUnit.Kilogram); |
| 96 | +``` |
| 97 | + |
| 98 | +Helper Construction Methods |
| 99 | +--- |
| 100 | +Construct measurements with various helper methods for convenience and readability. |
| 101 | +```C# |
| 102 | +Force.FromPressureByArea(Pressure p, Length2d area) |
| 103 | +Force.FromMassAcceleration(Mass mass, double metersPerSecondSquared) |
| 104 | +``` |
| 105 | + |
| 106 | +Precision and Accuracy |
| 107 | +=== |
| 108 | +A base unit is chosen for each unit class, represented by a double value (64-bit), and all conversions go via this unit. This means there will always be a small error in both representing other units than the base unit as well as converting between units. |
| 109 | + |
| 110 | +Units.NET was intended for convenience and ease of use, not highly accurate conversions, but I am open to suggestions for improvements. |
| 111 | + |
| 112 | +The tests accept an error up to 1E-5 for most units added so far. Exceptions include units like Teaspoon, where the base unit cubic meter is a lot bigger. In many usecases this is sufficient, but for others this may be a showstopper and something you need to be aware of. |
| 113 | + |
| 114 | +For more details, see [Precision](https://github.com/InitialForce/UnitsNet/wiki/Precision). |
| 115 | + |
| 116 | + |
| 117 | +What It Is Not |
| 118 | +=== |
| 119 | +* It is not an equation solver |
| 120 | +* It does not figure out the units after a calculation |
| 121 | + |
| 122 | +Want To Contribute? |
| 123 | +== |
| 124 | +This project is still early and many units and conversions are not yet covered. If you are missing something, please help by contributing or [ask for it](https://github.com/InitialForce/UnitsNet/issues) by creating an issue. |
| 125 | + |
| 126 | +Please read the wiki on [Adding a New Unit](https://github.com/InitialForce/UnitsNet/wiki/Adding-a-New-Unit). |
| 127 | + |
| 128 | +The repo uses [git-flow](https://github.com/nvie/gitflow) branch structure. |
| 129 | +In practice this means: |
| 130 | + * [Fork the repo](https://help.github.com/articles/fork-a-repo) as normal |
| 131 | + * Checkout the default **develop** branch. There is no master branch. |
| 132 | + * Do work on branches such as **feature/add-myunit** and **fix/34** |
| 133 | + * [Create a pull request](https://help.github.com/articles/using-pull-requests) as normal. |
| 134 | + |
| 135 | +Continuous Integration |
| 136 | +=== |
| 137 | +A [TeamCity build server](http://anj.no:8500/project.html?projectId=UnitsNet&tab=projectOverview&guest=1) performs the following: |
| 138 | +* Build and test pull requests. Notifies on success or error. |
| 139 | +* Build, test and publish nuget on commits to **stable** branch. |
| 140 | + |
| 141 | +[Contact me](https://github.com/anjdreas) if you have any questions. |
0 commit comments